CHAPTER 13 – COMPATIBILITY MODE – 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)

Post Comment
Login to post comments