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
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
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()
will already be living the first day of the rest of their lives together (see Figure 2
What Do These New Capabilities Mean to Developers?
|Figure 2: The wed() function in the Zend Development Environment|
Alright, so the ending to the story is now more idyllic, but what does it mean to PHP developers? It means quite a number of things. First, it means that your applications will run faster, because passing objects by reference requires much less data copying. For instance, when you send $joe
to a function, rather than creating a replica and copying his name, birth date, parents' name, list of former addresses, social security number and whatnot, PHP will only have to pass one object handle, one integer. Obviously, another direct result of this is that it saves a significant amount of memorystoring an integer requires much less space than storing a full-fledged replica of an object.
But perhaps more important, the new object model makes object oriented programming in PHP much more powerful and intuitive. No longer will you have to mess with cryptic &
characters to get the job done. No longer will you have to worry about whether changes you make to the object inside the constructor will survive the dreaded new
-operator behavior. No longer will you ever have to stay up until 2:00AM tracking elusive bugs! OK, maybe I'm lying with that last one; but seriously, the new object model significantly reduces the object-related stay-up-until-2:00AM type of bugs . In turn, that greatly increases the feasibility of using PHP for large-scale projects.