Wrap-Up and Future Directions

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 ...

Get Upgrading to PHP 5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.