TheGreek Posted April 11, 2014 Share Posted April 11, 2014 ok i have an index.php as so: <?phprequire '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 welcomeheaders is inc/index_h.phpfooters is inc/index_f.phpcontents 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); Quote Link to comment https://forums.phpfreaks.com/topic/287691-php-cant-see-class-member-included-with-the-include-keyword/ Share on other sites More sharing options...
trq Posted April 11, 2014 Share Posted April 11, 2014 Please, can you post some actual code using the forums [ code ][/ code ] tags so we can read it? Quote Link to comment https://forums.phpfreaks.com/topic/287691-php-cant-see-class-member-included-with-the-include-keyword/#findComment-1475751 Share on other sites More sharing options...
TheGreek Posted April 11, 2014 Author Share Posted April 11, 2014 Index.php <?php require 'php/stdlib.php'; $site->page->render(); foreach($page as $var => $value) { echo $var ." is ". $value." <br/>"; } ?> stdlib.php <?php //*************Site Initialization**********/// define("INC_DIR","inc/"); define("CSS_DIR","css/"); define("DEFAULT_HEAD",INC_DIR."index_h.php"); define("DEFAULT_FOOT",INC_DIR."index_f.php"); $currentPage=""; function __autoload($class) { include "classes/$class.php"; } function site_ini($site) { $siteprop = array( "siteName" => "Coredrilling Etc", "dbusername" => "root", "dbpassword" => "#######", "dbhostname" => "localhost", "defaultdbname" => "cce", "mtable" => "fed_tax_married", "stable" => "fed_tax_single" ); foreach($siteprop as $memb => $val ) { try{ $site->set($memb, $val); } catch(Exception $e){ { throw new Exception( 'Can Not set site Variable'.$memb, 0, $e); } } } } $site = new csite(); site_ini($site); $page = new cpage("welcome"); $site->setPage($page); ?> cpage.php <?php class cpage{ public $name; public $headers; public $footers; public $contents; private $writeback; public function get($property) { if (property_exists($this, $property)) { return $this->{$property}; } } public function set($property,$new_value) { if (!property_exists($this, $property)) { $this->{$property} = $new_value; } } public function __construct(){ $args = func_get_args(); if (func_num_args() == 1){ $this->name = $args[0]; $this->headers = DEFAULT_HEAD; $this->footers = DEFAULT_FOOT; } if(func_num_args() == 2){ $this->name = $args[0]; $this->pri_get_header($args[1]); $this->footers = DEFAULT_FOOT; } if (func_num_args()== 3){ $this->name = $args[0]; $this->pri_get_header($args[1]); $this->pri_get_footer($args[2]); } $this->pri_get_contents(); } public function render(){ include $this->headers; include $this->contents; include $this->footers; } public function get_headers(){ return $this->headers; } public function __toString() { return $this->name; } ////////PRIVATE FUNCTIONS private function pri_get_header($header){ $files = scandir(INC_DIR); foreach($files as $html_file) { if($html_file = $header){ $this->headers = INC_DIR . $html_file; } } } private function pri_get_footer($footer){ $files = scandir(INC_DIR); foreach($files as $html_file) { if($html_file = $footer){ $this->footers = HTML_DIR . $html_file; } } } private function pri_get_contents(){ $files = scandir(INC_DIR); foreach($files as $html_file) { if($html_file = $this->name.".php"){ $this->contents = INC_DIR . $html_file; } } } } ?> csite.php <?php class csite { public $page; public function _construct() { } public function get($property) { if (property_exists($this, $property)) { return $this->{$property}; } } public function set($property,$new_value) { if (!property_exists($this, $property)) { $this->{$property} = $new_value; } } public function setPage(cpage $page) { $this->page = $page; } public function render_page(){ $this->page->render(); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/287691-php-cant-see-class-member-included-with-the-include-keyword/#findComment-1475753 Share on other sites More sharing options...
trq Posted April 11, 2014 Share Posted April 11, 2014 I fail to see how you could expect the $page variable to be present in your header. $page is an instance of the class containing the render method that actually includes your header. Your code seriously makes little sense logically. Quote Link to comment https://forums.phpfreaks.com/topic/287691-php-cant-see-class-member-included-with-the-include-keyword/#findComment-1475759 Share on other sites More sharing options...
TheGreek Posted April 11, 2014 Author Share Posted April 11, 2014 If the object is created in the included stdlib.php which is in the index.php i assume that the call to render which has the include for the header made on the same page would make the object available to the header. I see that I was wrong I just have no idea how to recode it. My main goal is to make the header and footer repond dynamically depending on what page is displayed. So i could dynamically set the title or relevent css or or even page specific content. I even tried <script src="/scripts/jquery.js"></script> <script> $(document).ready(function () { document.title = "Hello World!"; }); </script> written into the index_h.php but nothing. so i thought was maybe it had something to do with the fact that it was being included. i'm at a loss right now. any suggestions on making the header content and footer dynamic. thanks for your time Quote Link to comment https://forums.phpfreaks.com/topic/287691-php-cant-see-class-member-included-with-the-include-keyword/#findComment-1475760 Share on other sites More sharing options...
TheGreek Posted April 11, 2014 Author Share Posted April 11, 2014 $page is an instance of the class containing the render method that actually includes your header. being that $page is created before the call to include the header make it available to the header Quote Link to comment https://forums.phpfreaks.com/topic/287691-php-cant-see-class-member-included-with-the-include-keyword/#findComment-1475761 Share on other sites More sharing options...
Solution TheGreek Posted April 11, 2014 Author Solution Share Posted April 11, 2014 @trq Thanks for your time I solved the issue. I was assuming that the Inculed file was linked to the index.php and then parsed but it is actually parsed in the instance of the class and the reult is linked to the index.php. Simply changing the <?php echo $page->name;?> in the index_h.php to <?php echo $this->name; ?> resolved the problem. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/287691-php-cant-see-class-member-included-with-the-include-keyword/#findComment-1475804 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.