Jump to content

Archived

This topic is now archived and is closed to further replies.

DrDre

Class function require() problem

Recommended Posts

PHP Platform: 4
I currently have a class with a function LoadResource($res)

That does [code]ob_start();
require_once($res);
ob_end_clean();[/code]
To be sure no headers are sent (This function is to be called on function includes and etc.

accessed ie $class->LoadResource('file.php');

Now heres the problem. My structure is in this format:

[code]
<?
require('configfile'); // this has info needed for constructing the class
//Say one variable is $theme that holds the theme the user wants to use
$class = new Class;
$class->LoadResource('test.php');
[/code]
test.php:
[code]<?
if($theme) die('Theme is set'); } else { die('theme is not set!!!!'); }
[/code]

Problem: file is loaded just fine, except the functions file does not have access to the variables initiated.
The example above prints theme is not set.


I was hoping using require inside of a class would act like a normal require, but its not :(

I know i COULD use global $theme in the LoadResource, but this code is to be used in many things, theres many variables, and the variable is to change per application.

So is there anyway I can make LoadResource() have access to read/set global variables?

Share this post


Link to post
Share on other sites
Grr... still not completely working.

the included file has access to the variables, but my index/other functions does not.

class.php:
[code]
class classx {
function LoadResource($res) {
   extract($GLOBALS);
   require_once($res);
}
}
[/code]
index.php:
[code]
$b = new classx;
$b->LoadResource('functions.php');
echo 'Variable:' . $variable; // prints nothing for $variable
test();  // prints nothing after variable:
[/code]
functions.php:
[code]
$variable=1;
echo 'File Variable: ' . $variable; // prints 1
function test() {
global $variable;
echo 'Function Variable:' . $variable; // prints nothing for var
}
[/code]

Share this post


Link to post
Share on other sites
You need to read up on [a href=\"http://au3.php.net/manual/en/language.variables.scope.php\" target=\"_blank\"]scope[/a]. Basicaly, objects have there own namespace, any variables or any files included within them are only available within each object.

Share this post


Link to post
Share on other sites
[!--quoteo(post=361577:date=Apr 4 2006, 10:47 AM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Apr 4 2006, 10:47 AM) [snapback]361577[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You need to read up on [a href=\"http://au3.php.net/manual/en/language.variables.scope.php\" target=\"_blank\"]scope[/a]. Basicaly, objects have there own namespace, any variables or any files included within them are only available within each object.
[/quote]
I understood that, Im asking how can I get past it :(
extract globals gives my included file access to variables
but any variables in the included file are not global, How can i make them global?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.