Comparing objects on PHP5

php.internals

Rui Hirokawa

22 years ago
Hi, I think objects comparison in PHP5 has problems. Here is small sample code, <?php class Foo { var $v = 1; } class Boo { var $v = 1; } $a = new Foo; $b = new Foo; $c = new Boo; $d = new Foo; $d->v = 2; $e = $a; // value comparison echo $a == $b ? "same," : "not same,"; echo $a == $c ? "same," : "not same,"; echo $a == $d ? "same," : "not same,"; echo $a == $e ? "same" : "not same"; echo "\n"; // value and type comparison echo $a === $b ? "same," : "not same,"; echo $a === $c ? "same," : "not same,"; echo $a === $d ? "same," : "not same,"; echo $a === $e ? "same" : "not same"; ?> The output from PHP4 and PHP5(ze1_compat on/off) is, PHP 4.3.6 : same,not same,not same,same same,not same,not same,same PHP 5.0.0RC2 zend.ze1_compatibility_mode=off : same,not same,not same,same (Notice: Object of class [Foo|Boo] could not be converted to integer) not same,not same,not same,same PHP 5.0.0RC2 zend.ze1_compatibility_mode=on : same,same,not same,same not same,not same,not same,not same The problem and question are, 1. the different class is recognised as same in PHP5 (ze1_compat=on). 2. the same object is recognised as different in PHP5 (ze1_compat=on). 3. Why integer casting of object was implicitly applied in PHP5 (ze1_compat=off) ?
-- Rui Hirokawa <rui_hirokawa@ybb.ne.jp>

Andi Gutmans

22 years ago
Did you check HEAD? I fixed something to do with the comparison. if it still doesn't work please let me know. Andi At 04:43 PM 5/9/2004 +0900, Rui Hirokawa wrote:

Derick Rethans

22 years ago
On Sun, 9 May 2004, Andi Gutmans wrote:
> Did you check HEAD? I fixed something to do with the comparison. if it > still doesn't work please let me know.
Shouldn't this example return "1\n1" ? <?php class Country { function set_name($name) { $this->name = $name; } } $jurop = new Country; $dutchieland = new Country; $dutchieland->set_name('The Netherlands'); echo (int) $jurop, "\n"; echo (int) $dutchieland, "\n"; And this, shouldn't return anything, right? <?php class Country { var $area; function Country($area) { $this->area = $area; } } $deutschland = new Country('West Europe'); $die_schweiz = new Country('West Europe'); $norway = new Country('Scandinavia'); if ($deutschland == $die_schweiz) { echo "Deutschland ist die Schweiz.\n"; } if ($deutschland == $norway) { echo "Deutschland er Norway.\n"; } This *was* working fine last week, except that I got notices like: Notice: Object of class Country could not be converted to integer in - on line 13 or the like. regards, Derick

Rui Hirokawa

22 years ago
I performed the same test with HEAD (PHP 5.0.0RC3-dev (build: May 9 2004 10:16:50)). There are no E_NOTICE message now, but, the problems are still existing with RC2, PHP 4.3.6 same,not same,not same,same same,not same,not same,same PHP 5.0.0-CVS 2004/5/9 (ze1_compatibility_mode = Off) same,not same,not same,same not same,not same,not same,same PHP 5.0.0-CVS 2004/5/9 (ze1_compatibility_mode = On) same,same,not same,same not same,not same,not same,not same Rui On Sun, 09 May 2004 19:17:35 +0300 Andi Gutmans <andi@zend.com> wrote:
> Did you check HEAD? I fixed something to do with the comparison. if it > still doesn't work please let me know. > > Andi > > At 04:43 PM 5/9/2004 +0900, Rui Hirokawa wrote: > > >Hi, > > > >I think objects comparison in PHP5 has problems. > >Here is small sample code, > > > ><?php > >class Foo { > > var $v = 1; > > } > > class Boo { > > var $v = 1; > > } > > $a = new Foo; > > $b = new Foo; > > $c = new Boo; > > $d = new Foo; > > $d->v = 2; > > $e = $a; > > > >// value comparison > > echo $a == $b ? "same," : "not same,"; > > echo $a == $c ? "same," : "not same,"; > > echo $a == $d ? "same," : "not same,"; > > echo $a == $e ? "same" : "not same"; > > echo "\n"; > >// value and type comparison > > echo $a === $b ? "same," : "not same,"; > > echo $a === $c ? "same," : "not same,"; > > echo $a === $d ? "same," : "not same,"; > > echo $a === $e ? "same" : "not same"; > >?> > > > >The output from PHP4 and PHP5(ze1_compat on/off) is, > > > >PHP 4.3.6 : > > same,not same,not same,same > > same,not same,not same,same > > > >PHP 5.0.0RC2 zend.ze1_compatibility_mode=off : > > same,not same,not same,same > > (Notice: Object of class [Foo|Boo] could not be converted to integer) > > not same,not same,not same,same > > > >PHP 5.0.0RC2 zend.ze1_compatibility_mode=on : > > same,same,not same,same > > not same,not same,not same,not same > > > > > >The problem and question are, > >1. the different class is recognised as same in PHP5 (ze1_compat=on). > >2. the same object is recognised as different in PHP5 (ze1_compat=on). > >3. Why integer casting of object was implicitly applied in PHP5 > > (ze1_compat=off) ? > >
-- Rui Hirokawa <rui_hirokawa@ybb.ne.jp>

Marcus Börger

22 years ago
Hello Rui, Monday, May 10, 2004, 1:24:33 AM, you wrote:
> I performed the same test with HEAD (PHP 5.0.0RC3-dev (build: May 9 > 2004 10:16:50)).
> There are no E_NOTICE message now, but, the problems are still existing with > RC2,
> PHP 4.3.6 > same,not same,not same,same > same,not same,not same,same
As long as we don't change this all is fine (because of BC).
> PHP 5.0.0-CVS 2004/5/9 (ze1_compatibility_mode = Off) > same,not same,not same,same > not same,not same,not same,same
The only think i am unsure is the first reult. But it makes pretty much sense the way it is now. It should compare all member variables and return equality if they are all equal. AFAIK it does so. The only difference to 4.3 then is the 5th result: class Foo { var $v = 1; } $a = new Foo; $b = new Foo; echo $a === $b ? "same," : "not same,"; In this example $a will be Object Id #1 and $b will become Object Id #2. Now tell me how those two could be identical? Every other resut than 'different' is simply bullshit.
> PHP 5.0.0-CVS 2004/5/9 (ze1_compatibility_mode = On) > same,same,not same,same > not same,not same,not same,not same
This should be the same as with 4.3. Since it obviously is not it must be fixed or the compatibility mode only provides weak compatibility and this will become a known issue. marcus

Andi Gutmans

22 years ago
This should be fixed now. Basically in non-compatibility mode == does the same as PHP 4 (it compares object type and properties) and === compares object handles (this is because comparing object handles from our experience with Java isn't used that often and it's very useful to be able to use the PHP 4 == semantics). In compatibility mode, both == and === compare object type and properties. In any case, overloaded internal extensions, such as SimpleXML can still overload the cast_object() handler to cast their objects to scalars. Andi At 08:24 AM 5/10/2004 +0900, Rui Hirokawa wrote: