Specifications
We are going to create a Page class. The main goal of this class is to limit the amount of
HTML needed to create a new page. It should allow us to alter the parts that change from page
to page, while automatically generating the elements that stay the same.
The class should provide a flexible framework for creating new pages and should not compro-
mise our freedom.
Because we are generating our page from a script rather than with static HTML, we can add
any number of clever things including functionality to enable the following:
• Enable us to only alter page elements in one place. If we change the copyright notice or
add an extra button, we should only need to make the change in a single place.
• Have default content for most parts of the page, but be able to modify each element
where required, setting custom values for elements such as the title and metatags.
• Recognize which page is being viewed and alter navigation elements to suit—there is no
point in having a button that takes you to the home page located on the home page.
• Allow us to replace standard elements for particular pages. If for instance, we want dif-
ferent navigation buttons in sections of the site, we should be able to replace the standard
ones.
Writing the Code for Your Class
Having decided what we want the output from our code to look like, and a few features we
would like for it, how do we implement it?
We will talk later in the book about design and project management for large projects. For
now, we will concentrate on the parts specific to writing object-oriented PHP.
Our class will need a logical name. Because it represents a page, it will be called Page. To
declare a class called Page, we type
class Page
{
}
Our class needs some attributes. We will set elements that we might want changed from page
to page as attributes of our class. The main contents of the page, which will be a combination
of HTML tags and text, will be called $content. We can declare the content with the following
line of code within the class definition:
var $content;
Object-Oriented PHP
C
HAPTER 6
6
OBJECT-ORIENTED
PHP
159
08 7842 CH06 3/6/01 3:34 PM Page 159