JavaScript Editor Javascript source editor     Web programming 



Team LiB
Previous Section Next Section

The New Object Model

Because this book did not emphasize 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's 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 between the object models used in PHP and Java.

  • Private and protected variables and methodsPrivate 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 8, "Working with Arrays and Objects," you learned that constructors are named for the class itself. In the new object 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 when run on a PHP 5 server.

  • Introduction of the _destructor() method This 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 handle the removal of the object from memory when it is no longer active. The destructor() method simply allows you to explicitly call for such action.

  • Abstract classes You may now 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 need to 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 This represents 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.

  • Implementation of the static, final, and constant keywords These can be 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 This 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 The new method of error-handling implements a Java-like mechanism, whereas error handling in PHP 4 is basically dependent on whatever the developer decided 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 8 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).

    Team LiB
    Previous Section Next Section


    JavaScript Editor Javascript source editor     Web programming