Jump to content

PHP cant' see class member included with the include keyword


TheGreek
Go to solution Solved by TheGreek,

Recommended Posts

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);

Link to comment
Share on other sites

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();
	}
}

?>
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Solution

@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

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.