Jump to content

No access to instance after include


bare_nature

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.