Classes Including and Variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mousy
Forum Newbie
Posts: 4
Joined: Thu Jun 29, 2006 7:39 am

Classes Including and Variables

Post by mousy »

I have a template engine class and a file includer class.

Basicly. I include the files first with the includer class.

But outside the class i can't refer to the variables that were included by the includer class outside the class.

Another problem will be once i have the variables avalible outside the class. How would i be able to use them in another class.

i know i could use global, but file will have different variables in so i cannot use it.

Please help :(
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

class Foo {
    public $bar;

    public function __construct() { }

    public function test()
    {
         echo $this->bar;
     }
}

$test = new Foo();
$test->bar = "Hello World!";

$test->test();
Output : Hello World!


Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
mousy
Forum Newbie
Posts: 4
Joined: Thu Jun 29, 2006 7:39 am

Post by mousy »

i understand the use of publick and private. But the variable is coming from a different location.

Code: Select all

function loadFiles(){
	if(is_dir($this->loadFrom) && $this->directory != '' && $this->loadFrom != ''){
		if(is_array($this->includes)){
			foreach($this->includes as $filename){
				if(file_exists($this->loadFrom.$filename)){
					include($this->loadFrom.$filename);
					$this->data[] = array($filename,$this->loadFrom.$filename);
				}
			}
		}
		else{
			while(($file = readdir($this->directory)) !== false){
				if(is_file($this->loadFrom.$file)){
			 		if(!preg_match('/^('.$this->excludes.')$/i', $file)){
						include($this->loadFrom.$filename);
						$this->data[] = array($file,$this->loadFrom.$file);
					}
				}
			}
		}
	}
}
That is the function inside the class that loads the file.
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

Don't understand the question then. You will need to rephrase what you are trying to do.
mousy
Forum Newbie
Posts: 4
Joined: Thu Jun 29, 2006 7:39 am

Post by mousy »

Right,

Code: Select all

$coreFiles = array('sitedata.php');
$precore = new fileFetch($libLocation);
$precore -> setIncludes($coreFiles);
$precore -> loadFiles();
if(is_array($precore -> getFiles())){
	$toLoad = array_merge($toLoad, $precore -> getFiles());
}
$precore -> fileEnd();
That loads the files in the array $coreFiles (if they exist).

So you see i have only one file (sitedata.php). The content of that file is.

Code: Select all

<?php

	$SiteWidth = 750;
	$Margins = 10;
	$SiteName = 'My site';
	$NavigationSeparator = ' // ';
	  
?>
In my index if i preform the first piece of code then go to echo $SiteWidth (which is included by the function in the class i posted) the variable doesn't echo anything.
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

If you include the file within a method (function) in your class, those variablse are only going to be available within that function.

Code: Select all

function foo()
{
    include_once 'sitedata.php';
}
Is basically the same as...

Code: Select all

function foo()
{
        $SiteWidth = 750;
        $Margins = 10;
        $SiteName = 'My site';
        $NavigationSeparator = ' // '; 
}
So thats why it doesn't work. You want to use constants rather for configuration options.

Code: Select all

define('SITE_WIDTH', '750');
    define('MARGINS', '10');
    define('SITE_NAME' , 'My Site');
    define('NAVIGATION_SEPARATOR', '//');
mousy
Forum Newbie
Posts: 4
Joined: Thu Jun 29, 2006 7:39 am

Post by mousy »

I would have never though of contants!

Thank you so much :D
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

You could load it like this:

Code: Select all

class FileFetch {

function loadData()
{
        foreach($this->includes as $filename){
              if(file_exists($this->loadFrom.$filename)){
                     include($this->loadFrom.$filename);
                     $this->data[$filename]['SiteWidth'] = $SiteWidth ;
                     $this->data[$filename]['Margins'] = $Margins;
                     $this->data[$filename]['SiteName'] = $SiteName;
                     $this->data[$filename]['NavigationSeparator'] = $NavigationSeparator;
              }
        }
}
(#10850)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

When using include (or variant) think of it as copy+paste on the fly.

As shown above, including a file within a function is just the same as copy+pasting the contents of the file inside the function.
Post Reply