Team LiB
Previous Section Next Section

Object-Oriented Programming (OOP)

Probably most of the backward-compatibility breaking changes in PHP 5 are in terms of its OOP support. Chapter 14, "Object-Oriented Programming in PHP," brought you up to date with the new features; here is a short recap about which of them might break old scripts.

The biggest change is also the one that could create problems with old code: passing objects. In PHP 4, this was done using values by default: An object was copied and this copy was then passed to a method. In PHP 5, this fundamentally changed. Now objects are passed by reference by default. That means that changing the object within the method it was passed to changes the original object. When the PHP core developers toured conferences with PHP 5 talks, this point created the most questions. Listing C.1 shows an example:

Listing C.1. Passing Objects
  class Programmer {
    var $name;

    function Programmer($s) {
      $this->name = $s;

  function GeekMode($p) {
    $p->name .= " aka Coogle";

  $p = new Programmer("John Coggeshall");
  echo("<p>They call him " . $p->name . "</p>");
  echo("<p>They also call him " . $p->name . "</p>");

In PHP 4, "They call him John Coggeshall" is printed out twice. However in PHP 5, the second time it says, "They call him John Coggeshall aka Coogle". This is because the Programmer object is passed to the function GeekMode() by reference. Thus, changes to this object are also visible within the main routine.

This is also important when cloning objects. The command $object2 = $object1 creates a reference to $object1 in $object2. If you want to create a copy that you can modify independently, use the new clone command:

$object2 = clone $object1;

Other OOP changes seldom appear in current applications, but are nevertheless worth mentioning:

  • Object casting using (int)$object, a nifty trick in PHP 4 to find out whether an object has properties set, creates a warning in PHP 5 (and always the same result, 1).

  • In PHP 5, comparing objects with == returns true only if the references point to the identical object.

The recommended way to migrate an existing application is to rewrite the OOP code in your PHP scripts to cooperate with the new rules. For instance, most PEAR modules have done so shortly after the first betas of PHP 5, to allow usage both with PHP 4 and PHP 5. However under certain circumstances, a workaround might be of good use. Using the php.ini directive zend.ze1_compatibility_mode, you can make PHP 5 behave like PHP 4 in terms of object handling. This directive can also be set using ini_set(), if you do not have access to your php.ini file (for example, on shared hosts) or want to use this behavior only on certain pages:

ini_set(zend.ze1_compatibility_mode, "On");


Another change with no new PHP 5 equivalent is that within classes, no direct assignment to $this is allowed any longer. However, in almost all cases, there is no need to do so anyway.

    Team LiB
    Previous Section Next Section