Page 1 of 1
Classes Including and Variables
Posted: Thu Jun 29, 2006 8:19 am
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

Posted: Thu Jun 29, 2006 8:24 am
by jamiel
Pimptastic | Please use 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
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]
Posted: Thu Jun 29, 2006 8:31 am
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.
Posted: Thu Jun 29, 2006 8:36 am
by jamiel
Don't understand the question then. You will need to rephrase what you are trying to do.
Posted: Thu Jun 29, 2006 8:41 am
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.
Posted: Thu Jun 29, 2006 8:54 am
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', '//');
Posted: Thu Jun 29, 2006 8:57 am
by mousy
I would have never though of contants!
Thank you so much

Posted: Thu Jun 29, 2006 10:36 am
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;
}
}
}
Posted: Fri Jun 30, 2006 6:29 am
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.