bare_nature Posted February 7, 2010 Share Posted February 7, 2010 The topic title is rather vague so let me explain my problem with a code snippet: <?php require_once("initialize.php"); include("header.php"); ?> body of the page <?php include("footer.php"); ?> In initialize.php I create an instance of a class (let's name it "Foo") so it is readily available to me by requiring initialize.php. So far so good. By including header.php I include the typical header information that is consistent throughout the website. However, in this header.php, I want to call the instance of Foo, I made, but it seems I have no access to that instance. How do I go about to have access to that instance of Foo in header.php? I hope everything is clear. Advise and suggestions are welcome! Bart Quote Link to comment https://forums.phpfreaks.com/topic/191251-no-access-to-instance-after-include/ Share on other sites More sharing options...
jl5501 Posted February 7, 2010 Share Posted February 7, 2010 If you create the instance in the main code stream in inialize.php then refer to the instance in the main code stream of header.php then it should be fine If, however the creation of the instance or the reference to it is inside a function, then it will not autoamtically be available as it will be out of scope Quote Link to comment https://forums.phpfreaks.com/topic/191251-no-access-to-instance-after-include/#findComment-1008384 Share on other sites More sharing options...
bare_nature Posted February 7, 2010 Author Share Posted February 7, 2010 I have access to the instance of the object (let's call id $foo) in the main stream, but not in header.php. My question is which approach I should take to have access to $foo in header.php (which is included and therefore not in the main stream)? Quote Link to comment https://forums.phpfreaks.com/topic/191251-no-access-to-instance-after-include/#findComment-1008422 Share on other sites More sharing options...
Buddski Posted February 7, 2010 Share Posted February 7, 2010 How are you accessing that variable? Is it inside a function? Try putting var_dump($foo); at the top of the header.php file and see what is output. Quote Link to comment https://forums.phpfreaks.com/topic/191251-no-access-to-instance-after-include/#findComment-1008425 Share on other sites More sharing options...
PFMaBiSmAd Posted February 7, 2010 Share Posted February 7, 2010 There are at least a dozen different reasons what you are doing is not working. You would need to post the actual code exhibiting the problem for anyone to even begin to have a chance at determine which one of those reasons is causing the problem in your code. xxxxxx out any sensitive information, but don't change any of the syntax or meaning of the code. Best guess is you are using a URL in your include statement for header.php and that code is not even being executed in the same scope as your main code. Quote Link to comment https://forums.phpfreaks.com/topic/191251-no-access-to-instance-after-include/#findComment-1008441 Share on other sites More sharing options...
bare_nature Posted February 7, 2010 Author Share Posted February 7, 2010 I use absolute paths, but to clarify everything I'll add some code snippets. In the main php page I first call require_once("initialize.php"), which pulls in all my classes including Foo. LIB_PATH is a constant and is the absolute path to the site root. DS is simply the directory separator. initialize.php: require_once(LIB_PATH.DS."foo.php"); foo.php: class Foo { private $random_variable = false; public function give_me_random_variable() { return $this->random_variable; } } $foo = new Foo(); With the above in mind, $foo is available on the main php page (I tested this thoroughly). However, my main php page is composed of a header and footer I include using a function that uses include with an absolute path to the site root. In the header I want to access $foo, but this is not possible for several reasons. header.php: if (isset($foo->give_me_random_variable())) { // Do stuff } The question is, how do I get access to $foo in header.php as it is not in the main flow of the page. Bart Quote Link to comment https://forums.phpfreaks.com/topic/191251-no-access-to-instance-after-include/#findComment-1008470 Share on other sites More sharing options...
bare_nature Posted February 7, 2010 Author Share Posted February 7, 2010 How are you accessing that variable? Is it inside a function? Try putting var_dump($foo); at the top of the header.php file and see what is output. The variable is not inside a function. A var_dump results in NULL as PHP tells me that the variable is undefined. At the moment, I don't have access to it form within the header.php. Thanks for your time, guys! Quote Link to comment https://forums.phpfreaks.com/topic/191251-no-access-to-instance-after-include/#findComment-1008479 Share on other sites More sharing options...
jl5501 Posted February 7, 2010 Share Posted February 7, 2010 How are you including header.php in your main page? Quote Link to comment https://forums.phpfreaks.com/topic/191251-no-access-to-instance-after-include/#findComment-1008484 Share on other sites More sharing options...
PFMaBiSmAd Posted February 7, 2010 Share Posted February 7, 2010 The only problem in the code posted so far is that isset() must be used on a variable. You cannot use it on a function call/class method, but you would already know that if that was your actual code because php's error reporting would show a fatal runtime error. Quote Link to comment https://forums.phpfreaks.com/topic/191251-no-access-to-instance-after-include/#findComment-1008488 Share on other sites More sharing options...
bare_nature Posted February 7, 2010 Author Share Posted February 7, 2010 You cannot use it on a function call/class method ... I don't see why this should be a problem since the function returns a variable. I do this quite often without any (fatal) problems. How are you including header.php in your main page? The include is done like this: include_layout_template("admin_header.php"); And the function "include_layout_template" looks like this: function include_layout_template($template="") { include(SITE_ROOT.DS.'public'.DS.'layouts'.DS.$template); } SITE_ROOT is a constant and DS is the directory separator. Quote Link to comment https://forums.phpfreaks.com/topic/191251-no-access-to-instance-after-include/#findComment-1008498 Share on other sites More sharing options...
jl5501 Posted February 7, 2010 Share Posted February 7, 2010 There is your answer then The include is done in a function so will not have access to $foo declared in clobal scope You could put global $foo; in your function and that would probably give you the pointer Quote Link to comment https://forums.phpfreaks.com/topic/191251-no-access-to-instance-after-include/#findComment-1008507 Share on other sites More sharing options...
bare_nature Posted February 7, 2010 Author Share Posted February 7, 2010 Stating it as global does indeed work. I think this will be my best option. Thanks for your input, guys! Bart Quote Link to comment https://forums.phpfreaks.com/topic/191251-no-access-to-instance-after-include/#findComment-1008571 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.