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.