DrDre Posted April 3, 2006 Share Posted April 3, 2006 PHP Platform: 4I 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? Link to comment https://forums.phpfreaks.com/topic/6516-class-function-require-problem/ Share on other sites More sharing options...
DrDre Posted April 3, 2006 Author Share Posted April 3, 2006 Woot found a use for Extract() :)Got it working. Link to comment https://forums.phpfreaks.com/topic/6516-class-function-require-problem/#findComment-23633 Share on other sites More sharing options...
DrDre Posted April 3, 2006 Author Share Posted April 3, 2006 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 $variabletest(); // prints nothing after variable:[/code]functions.php:[code]$variable=1;echo 'File Variable: ' . $variable; // prints 1function test() { global $variable; echo 'Function Variable:' . $variable; // prints nothing for var}[/code] Link to comment https://forums.phpfreaks.com/topic/6516-class-function-require-problem/#findComment-23641 Share on other sites More sharing options...
DrDre Posted April 4, 2006 Author Share Posted April 4, 2006 anyone :(? Link to comment https://forums.phpfreaks.com/topic/6516-class-function-require-problem/#findComment-23785 Share on other sites More sharing options...
trq Posted April 4, 2006 Share Posted April 4, 2006 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. Link to comment https://forums.phpfreaks.com/topic/6516-class-function-require-problem/#findComment-23826 Share on other sites More sharing options...
DrDre Posted April 4, 2006 Author Share Posted April 4, 2006 [!--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 variablesbut any variables in the included file are not global, How can i make them global? Link to comment https://forums.phpfreaks.com/topic/6516-class-function-require-problem/#findComment-23926 Share on other sites More sharing options...
DrDre Posted April 4, 2006 Author Share Posted April 4, 2006 [innocent bump] Link to comment https://forums.phpfreaks.com/topic/6516-class-function-require-problem/#findComment-23997 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.