13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Writing the Code for Your Class<br />

181<br />

puts a single menu button. If the button is to point to the page you are on, you display<br />

an inactive button instead, which looks slightly different <strong>and</strong> does not link anywhere.<br />

This way, you can keep the page layout consistent <strong>and</strong> provide visitors with a visual<br />

location.<br />

The operation IsURLCurrentPage() determines whether the URL for a button<br />

points to the current page.You can use several techniques to discover this information.<br />

Here, you use the string function strpos() to see whether the URL given is contained<br />

in one of the server set variables.The statement strpos( $_SERVER['<strong>PHP</strong>_SELF'], $url )<br />

returns a number if the string in $url is inside the superglobal variable<br />

$_SERVER['<strong>PHP</strong>_SELF'] or false if it is not.<br />

To use this Page class, you need to include page.inc in a script <strong>and</strong> call Display().<br />

The code in Listing 6.2 creates TLA Consulting’s home page <strong>and</strong> gives output similar<br />

to that previously generated in Figure 5.2.The code in Listing 6.2 does the following:<br />

1. Uses require to include the contents of page.inc, which contains the definition<br />

of the class Page.<br />

2. Creates an instance of the class Page.The instance is called $homepage.<br />

3. Sets the content, consisting of some text <strong>and</strong> HTML tags to appear in the page.<br />

(This implicitly invokes the __set() method.)<br />

4. Calls the operation Display() within the object $homepage to cause the page to<br />

be displayed in the visitor’s browser.<br />

Listing 6.2 home.php—This Home Page Uses the Page Class to Do Most of the Work<br />

Involved in Generating the Page<br />

<br />

You can see in Listing 6.2 that you need to do very little work to generate new pages<br />

using this Page class. Using the class in this way means that all your pages need to be<br />

very similar.<br />

If you want some sections of the site to use a variant of the st<strong>and</strong>ard page, you can<br />

simply copy page.inc to a new file called page2.inc <strong>and</strong> make some changes.This<br />

means that every time you update or fix parts of page.inc, you need to remember to<br />

make the same changes to page2.inc.<br />

A better course of action is to use inheritance to create a new class that inherits most

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!