[dx] Posted July 24, 2012 Share Posted July 24, 2012 Hi,I'm stuck here a lil bit. I have this. file1.php $var = 'something'; file2.php class init { function load_script() { require_once('file1.php'); }} file3.php include('file2.php');init::load_script(); So my problem is that $var is null in file3.php After loading file1.php from file2.php variable is visible, but I need it in file3.php Regards Link to comment Share on other sites More sharing options...
thescientist Posted July 25, 2012 Share Posted July 25, 2012 is the issue that the require_once is being scoped within the load_script method of the init class? Link to comment Share on other sites More sharing options...
[dx] Posted July 25, 2012 Author Share Posted July 25, 2012 Yes. Link to comment Share on other sites More sharing options...
boen_robot Posted July 25, 2012 Share Posted July 25, 2012 Maybe class init { static function load_script() { global $var;//Add other variables you wish to export to the global scope require_once('file1.php'); }} But better yet... why don't you ditch global variables in favor of static properties in the class?So that your class is something like: class init { static $var; static function load_script() { self::$var = 'something'; }} And in file3.php, you do include('file2.php');init::load_script();//Later, when you'd previously be using $varecho init::$var; Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now