Previous chapters demonstrate individual pieces of PHP 5, but this chapter is different. Instead of presenting new material, it combines together multiple features of PHP 5 into a unified program.
Specifically, I present a simple address book application that allows you to add new people and search for existing people. This application is designed to:
Demonstrate a wide range of new PHP 5 features
Work completely, even though it's limited in scope
Be accessible both over the web and from the command line
Separate application and presentation logic
Records are limited to first name, last name, and email address, and are stored in an SQLite database. A record can be turned into a Person object, and multiple Persons are stored in an addressBook object.
Since the application needs to work from both the Web and from the command line, you need an easy way to convert records into both HTML and plain text. To facilitate this task, every object is capable of using DOM to create an XML representation of its contents. You then use SimpleXML to manipulate these XML documents into formatted output.
The first section in this chapter covers the database schema, so you know how the records are stored. Parts two and three cover the Person and addressBook classes. These sections use many of PHP 5's new object-oriented features, including property overloading, visibility, class type hints, and custom object iterators using the IteratorAggregate interface. The classes also create, append, and import DOM documents and elements.
Now that you've defined how the data is represented, both in the underlying storage mechanism (the database) and within your program (the Person and addressBook classes), the next step is to translate the objects into user-visible content.
The fourth section shows how you create a set of templates, one for HTML and another for plain text. To ensure consistency, you use an abstract class to define the template interface for the page header, body, and footer. Within the templates, process the XML with SimpleXML.
Finally, you're ready to put together the actual application. However, since you've already created all the component parts, the assembly process is short. The biggest difficulty is converting input from both the Web and the command line into a similar format. One piece that is considerably simplified, however, is error handling through the use of exceptions.