What Makes PHP 5 Special?

PHP 5, released in July 2004, marks the maturing of PHP. The addition or fine-tuning of numerous object-oriented features brings you a better language in which to build sophisticated Web-based applications.
By default, PHP 5 passes objects by reference. To provide by-value functionality, PHP has a clone function for making a copy of an object if you need it. Passing by reference, though, is just passing a pointer around, which is more efficient than having to duplicate memory structures. This new version of PHP expands object-oriented support, such as providing the INSTANCEOF

keyword as well as better constructors and destructors, which were absent in previous versions. It also adds private and protected variables. Private variables are available to the object itself within member functions, while protected variables are available within object itself in member functions, and member functions of subclasses of the object, but are not available from code outside the class.

PHP 5 also introduces other common OOP features such as abstract classes, which allow you to build prototype classes; the FINAL keyword, which prevents subclassing of a member function; and the CONSTANT keyword, which defines a member variable that-surprise, surprise-is permanent. You’ll also find new, sophisticated exception handling with the TRY, CATCH, and THROW syntax. An error during the execution of your program means throwing an exception; for instance, you can use TRY when you do a division to protect against divide by 0. Your CATCH section can display a message saying, “You just tried to divide by zero inside routine X, and this shouldn’t happen.”

PHP now also supports function overloading, not to be confused with default values. With default values, PHP will use the default if you don’t specify a variable. Function overloading allows developers to create several different implementations of the same function, with different input variables. The beauty of this capability is that the engine will figure out which function you mean to use at runtime, depending on the type of variables with which you call it.

PHP 5 clearly has a lot to offer. If you’re one of many who have been clamoring for better object-oriented features, you’ll be happy with Version 5. And if you’ve hit a wall in the past with application complexity and PHP functionality, many of the new object-oriented features in PHP 5 are meant for you.

But isn’t avoiding bloat what PHP is all about? If I have lots of object-oriented code, isn’t it going to mean greater memory usage and, ultimately, slower code?

Yes and no. Bloat is really about loading code that doesn’t get used, whether it is libraries of your own making or part of PHP. This also goes for loading unnecessary data or making calculations that aren’t necessary. In each case, you, as a programmer, have control.

Here’s one example of how you can avoid this issue. Say you’re using the XXX class of PHP, but only in very particular situations. Instead of putting the REQUIRE statement right up at the top (which leads to cleaner, more readable code), you can put it immediately preceding the object. Given various conditionals that may never execute, that REQUIRE won’t get hit in many cases and therefore those classes won’t load. Problem solved.

LEAVE A COMMENT

Secured By miniOrange