The New Object Model
Because this book did not introduce you to programming in PHP from an object-oriented point of view, this section will not go into detail and show examples of the object model changes in PHP 5. For that, I highly recommend Harry Fuecks' article "PHP5: Coming Soon to a Webserver Near You", at http://www.sitepoint.com/article/1192/. This article contains an outstanding dissection of the object-related changes in PHP 5, and how to implement them.The following bullet points provide an overview of the changes. If you are a Java developer, you will begin to notice strong similarities.
- Private and protected variables and methods
Private variables and methods are only accessible from inside the class in which they are declared, while protected variables and methods are available to subclasses as well. This is a completely new feature in PHP 5 and thus presents no backward-compatibility issues other than you should go back and tighten up the security around your variables and methods. - Introduction of the _constructor() method
In Chapter 7, you learned that constructors are named for the class itself. In the new model, the _constructor() method is used, so that if you happen to change the name of the class, you don't need to change the name of the constructor as well. This is a completely new feature in PHP, but the PHP 4 way of doing things will still work just fine. - Introduction of the _destructor() method
Ensures an object is properly closed and "cleaned up" when it has run its course. This is a completely new feature in PHP, although in both PHP 4 and PHP 5, the built-in garbage collection routines will handle the removal of the object from memory when it is no longer active. The _destructor() method simply allows you to implicitly call for such action. - Abstract classes
Allow a group of subclasses to share a parent class while not directly using the parent class. For example, such a class would only be used to extend other classes, and would not be directly called. This is a completely new feature in PHP 5 and thus presents no backward-compatibility issues other than you might now want to go create a bunch of abstract classes where you previously could not. - Object overloading
The ability to call methods that are not explicitly declared within the class calling them. This is a completely new feature in PHP 5 and thus presents no backward-compatibility issues. - static, final, and constant keywords
are used to further define variables and methods within your classes. The static keyword allows variables and methods to be accessed without instantiating the class that contains them. The final keyword prevents variable or method in a class from being overridden by a subclass. The const keyword declares constants within a class, and only within the class. This is a completely new feature in PHP 5, although workarounds were present in PHP 4 for some of the same functionality. There are no backward-compatibility issues other than you may want to revisit your code and specifically use these keywords where possible. - Autoloading
Allows you to include a class, through the use of the _autoload() function, only when it is instantiated. This is a completely new feature in PHP 5 and thus presents no backward-compatibility issues. - Error handling
Implements a Java-like mechanism to handle errors, whereas error handling in PHP 4 is basically dependent on whatever the developer decides to code into place. Error handling in PHP 5 uses a try...catch control structure, like if...else for exceptions; the goal of which is to separate application data from errors. This is a completely new feature in PHP 5 and thus presents no backward-compatibility issues. However, if you have coded your own error handling into place, it is well worth converting to the try...catch structure.
As you can see, the object model has grown exponentially, and the changes are definitely for the better. While there's nothing wrong with the foundation information regarding objects that you learned about in Chapter 7, "Working with Arrays and Objects," that basic model is like driving a Le Car while PHP 5's object model is like driving a luxurious sedan (BMW, Mercedes, whatever you choose, as long as it's expensive).