When I compared two different objects, it returns firstly true
, and than after print_r
(on objects) returned false
.
From PHP manual:
Two object instances are equal if they have the same attributes and values, and are instances of the same class.
But here, in example, I set different values. Why the result is different between PHP 5.4.0 - 5.5.7?
abstract class first { protected $someArray = array(); } class second extends first { protected $someArray = array(); protected $someValue = null; public function __construct($someValue) { $this->someValue = $someValue; } } $objFirst = new second('123'); $objSecond = new second('321'); var_dump ($objFirst == $objSecond); print_r($objFirst); var_dump ($objFirst == $objSecond);
Result is:
bool(true) second Object ( [someArray:protected] => Array ( ) [someValue:protected] => 123 ) bool(false)
But what I expected was:
bool(false) second Object ( [someArray:protected] => Array ( ) [someValue:protected] => 123 ) bool(false)
This was a bug in PHP. It's fixed now, see the commit. In short:
properties_table
of the object ends up having a NULL value.properties_table
at the same index - reporting the objects as equal. That doesn't make sense of course, because it discards all differences in the following properties. This is fixed now.print_r
changes the result, is that by fetching the properties of the object (get_properties
) the properties hashtable is rebuilt (rebuild_properties_table
) which uses entirely different (and correct) comparison code.properties_table
and properties
are two different ways PHP uses to represent properties - the former being way more efficient and used for declared properties and the latter used for dynamic properties. The print_r
call effectively makes the object properties dynamic.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With