Although you can write a similar address book application in PHP 4, this version is better encapsulated, doesn't need to worry about passing objects as references, has simplified error handling, and makes it easy to use XML.
In PHP 4, there's no concept of visibility, so all object properties and methods are public. However, in PHP 5, you're able to effectively wall off public properties as protected data, yet still allow people easy access using _ _get( ) and _ _set( ).
By making _ _set( ) accept only the properties defined in the object constructor, you're able to limit exactly which properties are and aren't valid. This allows you to iterate through properties, creating SQL statements and DOM objects, without worrying that you'll encounter some unexpected data.
Additionally, the application takes advantage of PHP 5's automatic pass-by-reference behavior for objects. After you insert a new Person into the database, you can update its id property within the addPerson( ) method and have this change affect the original object.
The program also uses exceptions to simplify error handling. DOM automatically throws exceptions for any errors mentioned in the DOM specification. SQLite also throws exceptions from its constructor. By manually throwing a few additional SQLiteExceptions, you consolidate all the error handling into a single try/catch block.
Finally, the interoperability of the new PHP XML extensions allows you to easily create documents with DOM, yet access them for printing using SimpleXML. Furthermore, it's no problem to substitute XSLT for SimpleXML within your templates, or to search an addressBook object using XPath.
However, the address book application created in this chapter is just a start, providing a solid foundation for future enhancements. For instance, it would be nice to allow people to edit and delete entries from the address book, or to enlarge the number of fields from beyond first name, last name, and email address.
Also, as mentioned earlier, this application doesn't print a friendly error message when an exception is thrown. One solution is to modify Template to provide a getErrorMessage( ) method, which displays format-appropriate text.
Another nifty feature would be enabling this application as a web service, using either SOAP or REST. You read about SOAP in Chapter 9. REST is a different approach to web services, where you make requests using HTTP methods such as GET and POST, and the method type tells the server what action it should take. For example, GET tells the server you want to retrieve existing data, whereas POST means you want to update existing data. The server then replies with the results in an XML document that you can process.
Since the application already uses XML internally, it should be a simple extension to create an xmlTemplate. For more on REST, see the REST Wiki at http://internet.conveyor.com/RESTwiki/.
Finally, you can add the ability to read and write other address book formats. John E. Simpson has an article on XML.com that discusses many different XML address book schemas, such as vCard. It's available at http://www.xml.com/pub/a/2004/03/31/qa.html.