简体   繁体   中英

PHP- can't see class member after include

ok i have an index.php as so:

<?php
require 'php/stdlib.php';


$site->page->render();


 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(){

    $this->page->render();
}

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>
 <html>
 <head>
 <meta charset="UTF-8">
 <title><?php echo $page->name; ?></title>
 <script src="/scripts/jquery.js"></script>

 </head>

 <body>

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();   
site_ini($site);   
$page = new cpage("welcome");  
$site->setPage($page);

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 PHP.net documentation on the function: http://www.php.net/manual/en/function.include.php

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM