Section 13.4. Compatibility Mode


13.4. Compatibility Mode

In an earlier section, one suggested solution was to turn on the compatibility mode. This mode actually changes more behavior than just the pass-by-reference behavior. It also affects other Zend Engine 2 (PHP 5) related changes. Turning on Zend Engine 1 (PHP 4) compatibility mode changes the following:

  • Passing objects to a function results in a copy of the object (discussed in the previous section).

  • Casting an object to a Boolean, integer, or double results in 0 if the object has no properties.

  • Comparing objects results in true when the properties of the two objects have the same content.

13.4.1. Casting Objects

In PHP 4, (int) $object returns 1 if the object has properties or 0 if the object has no properties. This is deprecated in PHP 5, where (int) $object always results in a 1. The following example shows this behavior:

 <?php     /* Turn error reporting off */     error_reporting(0);     class bagel {     }     $b = new bagel();     /* Cast to an integer */     if ((int) $b) {         echo "Groovy baby!\n";     }     /* Turn on compatibility mode and cast to an integer */     ini_set('zend.ze1_compatibility_mode', 1);     if ((int) $b) {         echo "Yeah baby!\n";     } ?> 

In PHP 4, this example results in no output. However, in PHP 5 the output is

 Groovy baby! 

13.4.2. Comparing Objects

The results when you compare objects with the == operator changed in PHP 5. In PHP 4, if all the objects' properties are the same, comparing objects returns true. In PHP 5, the equality operator only returns TRue if the objects are really the same, which means that they have the same object handle. Compatibility mode turns on the old PHP 4 way of comparing objects:

 <?php     class bagel {         var $topping;         function bagel($topping)         {             $this->topping = $topping;         }     }     class icecream {         var $topping;         function icecream($topping)         {             $this->topping = $topping;         }     }     /* Instantiate the bagel and ice cream */     $bagel = new bagel('chocolate');     $icecream = new icecream('chocolate');     /* In Zend engine 2 this comparison will return false */     if ($bagel == $icecream) {         echo "A bagel is the same as icecream! (1)\n";     }     /* If we turn on compatibility mode, it will return true */     ini_set('zend.ze1_compatibility_mode', 1);     if ($bagel == $icecream) {         echo "A bagel is the same as icecream! (2)\n";     } ?> 

This example shows that the compatibility mode makes a bagel the same as ice cream, as long as the topping is the same:

 A bagel is the same as icecream! (2) 



    PHP 5 Power Programming
    PHP 5 Power Programming
    ISBN: 013147149X
    EAN: 2147483647
    Year: 2003
    Pages: 240

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net