Hey, I've been working on what should have been a simple problem for about a week now, and I could use some help.
I'm learning how to use the OO features of PHP5, and I eventually want to make my entire web site using OOP. To make it easy on myself I decided to copy verbatim the script from hudzilla that had a simple OO website script ready to go. To see if I understood what I learned, I decided to add functionality allowing me to have dynamic titles. Let me show you the script...
<?php
include 'stdlib.php';
$site = new csite();
initialise_site($site);
// This is the part I want to be able to edit easily, and then have it printed to the header.php file
$page = new cpage("This is a title");
$site->setPage($page);
$content = <<<EOT
Welcome to my personal web site
EOT;
$page->setContent($content);
$site->render();
?>
This is the index.php file. stdlib.php is used to autoload the header and footer files, which are called from inside csite (hence the type hinting).
<?php
function __autoload($class) {
include "$class.php";
}
function initialise_site(csite $site) {
$site->addHeader("header.php");
$site->addFooter("footer.php");
}
?>
Simple stuff there.
<?php
class csite {
private $headers;
private $footers;
private $page;
public function __construct() {
$this->headers = array();
$this->footers = array();
}
public function __destruct() {
// clean up here
}
public function render() {
foreach($this->headers as $header) {
include $header;
}
$this->page->render();
foreach($this->footers as $footer) {
include $footer;
}
}
public function addHeader($file) {
$this->headers[] = $file;
}
public function addFooter($file) {
$this->footers[] = $file;
}
public function setPage(cpage $page) {
$this->page = $page;
}
}
?>
csite.php most importantly defines the render() function which is used in the index.php file to include the header.php file, show the data held in the $content variable, then include the footer. The $content variable is defined in the cpage.php file.
<?php
class cpage extends csite {
private $content;
private $title;
public function __construct($title) {
$this->title = $title;
}
public function __destruct() {
// clean up here
}
public function showTitle() {
echo $this->title;
}
public function render() {
echo $this->content;
}
public function setContent($content) {
$this->content = $content;
}
}
?>
This is where page specific goodies should be in, so of course content is rendered using the render() function in the cpage.php file. As you can see, the __constructor function sets the value of $this->title to what is entered into the parenthesis of the cpage object ($page = new cpage("This is a title"). This is perfect, except for the part that is most important, printing that $title into the <title> tag in the header.php file.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title><?php $page->showTitle(); ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="css/default.css" />
</head>
<body>
Here is the problem, the $page->showTitle(); part causes a fatal error to occur. The error is something like "undefined function showTitle()". I suspect that the header.php file is getting included BEFORE the new cpage object is defined in the index.php file, therefore there is no showTitle() function for the header.php file to call, therefore there is an error.
If I monkey around with it, I can get the error to stop, but the function is still never used, as if there is no title for it to print. But, if I do this...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title><?php $page = new cpage("This is a title"); $page->showTitle(); ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="css/default.css" />
</head>
<body>
... Then it works as expected. But I don't want to have different headers for each page! I want to have one header that I can define a title for based on what is in the index.php file (or any other new page for that matter).
See my problem? I hope someone can help me here.