![]() | |
| Figure 1: PHP Usage as of October, 2002 |
Objects in the Old Days
So, what could one do with objects back in the days of PHP 3.0 or, for that matter, with the current PHP 4.0 version? Not much, really. Objects were essentially containers for properties, like associative arrays. The biggest difference was that objects had to belong to a class. Classes, as in other languages, contained a collection of both properties and methods (functions). You could instantiate instances (objects) from the classes using the new operator. The object model supported single inheritance, which let users extend (specialize) an existing class without having to write it from scratch or copy it. Finally, PHP 4.0 also added the ability to call methods of a specific class, from both within and outside of object contexts.
One of the biggest twists in PHP's history was that despite the very limited functionality, and despite a host of problems and limitations, object oriented programming in PHP thrived and became the most popular paradigm for the growing numbers of off-the-shelf PHP applications. This trend, which was mostly unexpected, caught PHP in a sub-optimal situation. It became apparent that objects were not behaving like objects in other OO languages, and were instead behaving like associating arrays.
The Limitations of the Old Object Model
The most problematic aspect of the PHP 3 / PHP 4 object model was that objects were passed around by value, and not by reference. What does that mean? Here's an example.
Let's say you have a simple, somewhat useless function, called myFunction():
function myFunction($arg)
{
$arg = 5;
}
And you call this function as follows:$myArgument = 7; myFunction($myArgument); print $myArgument;As you probably know, the call to myFunction() will leave $myArgument unchanged; Sent to myFunction() is a copy of $myargument's value, and not $myargument itself. This type of argument passing is called passing arguments by value. Passing arguments by reference is done by most structured languages and is extremely useful, as it allows you to write your functions or call other people's functions without worrying about side effects they may have on variables outside their scope.
function wed($bride, $groom)
{
if ($bride->setHusband($groom)
&& $groom->setWife($bride)) {
return true;
}
else {
return false;
}
}
wed($joanne, $joe);
print areMarried($joanne, $joe);
| Author Note: Implementing Woman::setHusband(), Man::setWife() and areMarried() is left as an exercise for the reader. |
function wed(&$bride, &$groom)With this new implementation of wed(), Joanne and Joe would have better luck (or not, depending on your point of view).
The AnswerZend Engine 2
Being finally convinced that objects are indeed special creatures and deserve their own distinct behavior, was only the first step. We had to come up with a way of doing this without interfering with the rest of the semantics of PHP, and preferably, without having to rewrite the whole of PHP itself. Luckily, the solution came in the form of a big light bulb that emerged above Andi Gutmans' head just over a year ago. His idea was to replace objects with object handles. The object handles would essentially be numbers, indices in a global object table. Much like any other kind of variables, they will be passed and returned by value. Thanks to this new level of indirection we will now be moving around handles to the objects and not the objects themselves. In effect, this feature means that PHP will behave as if the objects themselves are passed by reference.
Let's go back to Joe and Joanne. How would wed() behave differently now? First, $joanne and $joe will no longer be objects, but rather, object handles, let's say 4 and 7 respectively. These integer handles point to slots in some global objects table where the actual objects reside. When we send them to wed(), the local variables $bride and $groom will receive the values 4 and 7, setHusband() will change the object referenced by 4, setWife() will change the object referenced by 7, and when wed() returns, $joanne and $joe will already be living the first day of the rest of their lives together (see Figure 2).
![]() | |
| Figure 2: The wed() function in the Zend Development Environment |
What Else Is New?
As one might expect, the Zend Engine 2 packs in quite a few other features along with its brand new object model. Some of the features further enhance object-oriented capabilities, such as private member variables and methods, static variables and language-level aggregation. Most notable is the revolutionized interaction with external component models, such as Java, COM/DCOM and .NET via overloading.
PHP 4.0 was the first version to introduce this sort of integration, but the new implementation is much quicker, more complete, more reliable and far easier to maintain and extend. These elements mean that PHP 5.0 will play very nicely in your existing Java or .NET based setup. You'll be able to use your existing components inside PHP transparently, as if they were regular PHP objects. Unlike PHP 4.0, which had a special implementation for such overloaded objects, PHP 5.0 uses the same interface for all objects, including native PHP objects. This feature ensures that PHP objects and overloaded objects behave in exactly the same way.
Finally, the Zend Engine 2 also adds exception handling to PHP. To date, the sad reality is that most developers write code that does not handle error situations gracefully. It's not uncommon to see sites that spit out cryptic database errors to your browser, rather than displaying a more user-friendly "An error has occurred" message. A key reason for this is that handling error situations with the versions of PHP was a daunting taskyou actually had to check the return value of each and every function. Adding a set_error_handler() function made managing errors slightly easier, but still left a lot to be desired. Adding exception handling to PHP lets developers achieve fine-grained error recovery, but more importantly, it facilitates graceful application-wide error recovery.
The release of PHP 5.0, powered by the Zend Engine 2.0, will mark a significant step forward in PHP's evolution as one of the key Web platforms in the world today. While keeping its firm commitment to users who prefer using the functional structured syntax of PHP, the new version will provide a giant leap ahead for those who are interested in its object oriented capabilitiesespecially for companies developing large scale applications.
| DevX is a division of Jupitermedia Corporation © Copyright 2007 Jupitermedia Corporation. All Rights Reserved. Legal Notices |