I have created a Page class and all the pages on my website are objects of that Page class. Each Page object has a title attribute which is passed in as an argument to the constructor and so creating a new Page object looks as follows.
<?php
require_once('library/Page.php');
$page = new Page('About');
$page->header();
This is where the problem arises.
The header() method is…
public function header()
{
require_once($this->templatePath . 'header.php');
}
I then echo the page title using
<?php echo $page->title; ?>
However, I get the following error.
Notice: Undefined
variable: page in
/Users/aaronfalloon/Projects/PHP/tfm/template/header.php
on line 19Notice: Trying to get property
of non-object in
/Users/aaronfalloon/Projects/PHP/tfm/template/header.php
on line 19
Let me further explain about what Gumbo has written.
When you included the template file, you did the operation WITHIN the header function, thus making all $page variable in the template file referring to the local $page variable in the header function, which apparently is not declared/defined.
You should use $this->title instead to refer to the current class.
It is something like
when you try to include the template file:
.