Specifications
2. Creates an instance of the class Page. The instance is called $homepage.
3. Calls the operation SetContent() within the object $homepage and pass some text and
HTML tags to appear in the page.
4. Calls the operation Display() within the object $homepage to cause the page to be dis-
played in the visitor’s browser.
LISTING 6.2 home.php—This Homepage Uses the Page Class to Do Most of the Work
Involved in Generating the Page
<?
require (“page.inc”);
$homepage = new Page();
$homepage -> SetContent(“<p>Welcome to the home of TLA Consulting.
Please take some time to get to know us.</p>
<p>We specialize in serving your business needs
and hope to hear from you soon.</p>”
);
$homepage -> Display();
?>
You can see in Listing 6.2 that we need to do very little work to generate new pages using this
Page class. Using the class in this way means that all our pages need to be very similar.
If we want some sections of the site to use a variant of the standard page, we can simply copy
page.inc to a new file called page2.inc and make some changes. This will mean that every
time we updated or fixed parts of page.inc, we will need to remember to make the same
changes to page2.inc.
A better course of action is to use inheritance to create a new class that inherits most of its
functionality from Page, but overrides the parts that need to be different.
For the TLA site, we want to require that the services page include a second navigation bar.
The script shown in Listing 6.3 does this by creating a new class called ServicesPage which
inherits from Page. We provide a new array called $row2buttons that contains the buttons and
links we want in the second row. Because we want this class to behave in mostly the same
ways, we only override the part we want changed—the Display() operation.
Using PHP
P
ART I
166
08 7842 CH06 3/6/01 3:34 PM Page 166