Jump to content

Script won't include var


[dx]
 Share

Recommended Posts

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...