PHP- can't see class member after include

ok i have an index.php as so:

require 'php/stdlib.php';


 foreach($page as $var => $value) {
    echo $var ." is ". $value." <br/>";

the obj creation for site and page is in the stdlib file and is obviously working cuz the -for each- loop prints out:

name is welcome 
headers is inc/index_h.php 
footers is inc/index_f.php 
contents is inc/welcome.php 

It show that the object is created. I also did a var dump with proper results here is site---page---render:

   public function render_page(){


here is page---render:

 public function render(){

        include $this->headers;
        include $this->contents;
        include $this->footers;

however the result of the script is the following:

Undefined variable:

and also

Trying to get property of non-object: both errors point to my $page object that i used in the include file for the page header:

 <!DOCTYPE html>
 <meta charset="UTF-8">
 <title><?php echo $page->name; ?></title>
 <script src="/scripts/jquery.js"></script>



The errors actually print out in the html title tag not on the screen meaning i have to use View Source on my browser to see it How do i get the $page object to be visible when using an include Im usually pretty good about finding answers myself but this thing has me stumped for two days now.(I have learned alot about many other things while searching for answer tho so I guess not all is lost) If anyone could help me I would greatly appreciate it.

Probably should have added that the page and site object are instantiated in stdlib.php with the following

$site = new csite();   
$page = new cpage("welcome");  


When you use PHP's build-in include function, the contents of the included file are executed in the same scope as the call to include. Therefore, you really want to call $this in the included files, as their code is executed as-though it were actually written inside the render() method, and you will note that no $page variable was declared in that method.

On the other-hand, it may make more syntactic sense to set $page = $this prior to the first include.

I up-voted for you because, while the title does not suggest it, you are actually highlighting an important aspect of the PHP include and require functions: the way they pass scope.

For more information about this topic, have a close read of the documentation on the function:

Edit: To clarify why one example works, and another does not, it is because $page, from the include, has the same scope as your foreach, but is not declared inside the render.

Do not pull it in as a global, however. As syntactically you want to render the page you call render on, so: $this

Need Your Help

Post message from Thread to GUI best practice?

multithreading delphi delphi-7 messaging

I am working on small monitoring application which will have some threads for communication with some devices via SNMP, TCP, ICMP, other threads have to perform some calculations.

Automated tests with Selenium: how to close alert-Windows?

multithreading selenium alert

Excecuting an automated test on a website with selenium I was faced with this problem, that somewhere on the website, when my test changes the selection of a DropDownList, an alert-window is thrown