Object oriented pages provide a ton of flexability and ease of code maintenance, so its definitely a great avenue to venture down if your looking into it.
Ill start off with a very simple example of a decent document object model.
This will require PHP 5 in order to work.
Below is a list of files and their appropriate path in the directory structure.
site/runtime.php
<?php
// Require our dom (document object model) runtime files
require_once "dom/page.php";
require_once "dom/navbar.php";
require_once "dom/text.php";
?>
site/dom/text.php
<?php
class PageText
{
function PreRender()
{
}
function DoRender()
{
}
function PostRender()
{
}
}
class PageContentBox
{
var $title;
function PageContentBox($title)
{
$this->title = $title;
}
function PreRender()
{
?>
<b>PageContent: <?=$this->title?></b>
<br>
<?php
}
}
?>
site/dom/navbar.php
<?
class NavBarElement
{
var $title, $link;
function NavBarElement($title, $link)
{
// This is our constructor (Note how this method's name is NavBarElement, same as the class name - This means its a constructor)
$this->title = $title;
$this->link = $link;
}
function DoRender()
{
// Render this navigation bar element
echo '<a href="' . urlencode($this->link) . '">' . $this->title . '</a>';
}
}
class PageNavBar
{
var $items = array();
function Add($title, $link = '')
{
// Add a new navigation bar element
$this->items[] =& new NavBarElement($title, $link);
}
function DoRender()
{
// Render our navigation bar elements individually
$count = sizeof($this->items);
// Separate each one with a hyphen, so render them all except the last one, as we wont include a hyphen for that one
for ($i = 0; $i < $count - 1; $i++)
{
$this->items[$i]->DoRender();
echo ' - ';
}
// Render our last item while not rendering a separator
$this->items[$count - 1]->DoRender();
}
}
?>
site/dom/page.php
<?
class Page
{
var $navbar;
var $title;
var $children = array();
function Page($title = "")
{
// Set our page title
$this->title = $title;
// Instance a new PageNavBar object
$this->navbar =& new PageNavBar();
// Add some navigation elements to our navbar
$this->NavBar()->Add("News", "/index.php");
$this->NavBar()->Add("About", "/about.php");
$this->NavBar()->Add("Contact Us", "/contact.php");
}
function& NavBar()
{
// This method simply returns a reference to our private $navbar variable
return $this->navbar;
}
function Add(&$object)
{
// This method adds a new object to be rendered when we render the page
$this->children[] =& $object;
}
function Render()
{
/* This is the main render function
When this Render method is called, we loop through all of our children objects and ask them to render too
This is done in order, so objects which were added first are also rendered first
Additionally, we have a few stages of document render.
Initially, we render the page header. Then we loop through each of our children objects.
We ask them to conduct their PreRender code, if any. Then their DoRender code, then their PostRender code, if any
This allows each of the objects to decide if they should render differently (or at all) by giving them a few passes over their code.
This means the order of the code in the actual document is not that important, as we can, for example, retrieve a list of information from a database in one object's pre-render code, and then decide if we should render it in the DoRender portion
*/
// Ask the page to render the header
$this->Header();
// Loop through all child objects
foreach ($this->children As $child)
{
// If this child has a PreRender method, call it
if (method_exists($child, 'PreRender'))
$child->PreRender();
// All children are required to have a DoRender method, so call it
$child->DoRender();
// If the child has any PostRender code, call it
if (method_exists($child, 'PostRender'))
$child->PostRender();
}
// Finally, render our page footer
$this->Footer();
}
function Header()
{
// Render header. Note the use of $this->title in the <title> tag to fetch the page title
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html>
<head>
<title><?=$this->title?></title>
</head>
<body>
<p>This is the page header!</p>
<hr>
<?php
// Ask our navigation bar object to render itself
$this->NavBar()->DoRender();
?>
<hr>
<p>Here is some page content:</p>
<?
}
function Footer()
{
// Render footer, also close off the body and html tags
?>
<p>Page footer.</p>
© Copyright information here, or other standard footer links
</body>
</html>
<?php
}
}
?>
Finally, an example index.php page:
index.php
<?php
// Require our page runtime file, which in turn includes other files we need
require "site/runtime.php";
// Create a new page object with the title 'Test Page'
$page =& new Page("Test Page");
class IntroText extends PageText
{
function DoRender()
{
?>
This is my page introduction text.
<br><br>
<?php
}
}
class BodyText extends PageContentBox
{
function DoRender()
{
?>
Here is some content text, its got a bold heading.
<?php
}
}
// Add our introtext and bodytext class modules we created above
// Try switching around these two lines - it will change the order they are rendered in
$page->Add(new IntroText);
$page->Add(new BodyText("Content Heading Here"));
// Render our page - Comment this out and a blank page will be shown!
$page->Render();
?>
That should get you started, it shows how to construct a document from objects and shows a few different techniques you should be interested in.