Re: [PHP-DEV] Object comparison
On 11/08/2012 05:07 PM, Sara Golemon wrote: From: http://php.net/manual/en/language.operators.comparison.php An object compared to anything which is not a bool, null, or object should result in the object appearing to be greater than the other operand. For example: $a = new stdClass(); $b = new stdClass(); var_dump(null $a); var_dump(false $a); var_dump(true == $a); var_dump($a == $b); var_dump(0 $a); var_dump(1 $a); // false var_dump(2 $a); // false var_dump(foo $a); var_dump(2 $a); var_dump(tmpfile() $a); Based on docs, I expect all nine of these to yield true, however in practice, the two marked false come out as false because the RHS object is converted to an integer (1), contrary to the docs. Doc bug? Or code bug? I'm inclined to call it a code bug, but wanted others' thoughts. -Sara You seem to be reporting a bug (contrary to the docs), but you might be suggesting that the doc be corrected (since doc and behaviour should always match). I would suggest being very careful about implementing something as central as a change to how comparisons are made, that ought to be approached with great caution because it has the potential for breaking a huge amount of existing code. [It does occur to me however, simply as a point of interest, that objects traditionally have both a constructor and a destructor, and the idea of objects also having a comparator makes a certain amount of sense; a default object comparator might implement the currently defined comparisons while allowing derived classes to override the default behaviour and leaving comparisons not involving objects undisturbed.] Comparing objects to scalars is really an apples/oranges comparision to begin with. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP-DEV] Object comparison
I would like to place a suggestion for comparing objects (I hope it is no problem, because this does not have anything to do with Sara's question - but it came to my mind when I read her mail). It would be a great feature if objects could be compared to other objects with , and the other operators, like it is suggested in RFC https://wiki.php.net/rfc/comparable The DateTime class offers this feature - it would be nice if this could be made usable for userland classes/objects, too. Best regards Christian Stoller -Original Message- From: p...@golemon.com [mailto:p...@golemon.com] On Behalf Of Sara Golemon Sent: Friday, November 09, 2012 1:07 AM To: PHP internals Subject: [PHP-DEV] Object comparison From: http://php.net/manual/en/language.operators.comparison.php An object compared to anything which is not a bool, null, or object should result in the object appearing to be greater than the other operand. For example: $a = new stdClass(); $b = new stdClass(); var_dump(null $a); var_dump(false $a); var_dump(true == $a); var_dump($a == $b); var_dump(0 $a); var_dump(1 $a); // false var_dump(2 $a); // false var_dump(foo $a); var_dump(2 $a); var_dump(tmpfile() $a); Based on docs, I expect all nine of these to yield true, however in practice, the two marked false come out as false because the RHS object is converted to an integer (1), contrary to the docs. Doc bug? Or code bug? I'm inclined to call it a code bug, but wanted others' thoughts. -Sara -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Object comparison
Hi, Maybe it goes way to far, but there is a PECL-extension [1], that allows to overload every(?) operator. However, it seems to be unmaintained for 6 years now and will probably not work anymore, but it may be usable as a starting point. Python provides this too [2] Regards, Sebastian [1] http://pecl.php.net/package/operator [2] http://docs.python.org/2/reference/datamodel.html#special-method-names 2012/11/9 Christian Stoller stol...@leonex.de I would like to place a suggestion for comparing objects (I hope it is no problem, because this does not have anything to do with Sara's question - but it came to my mind when I read her mail). It would be a great feature if objects could be compared to other objects with , and the other operators, like it is suggested in RFC https://wiki.php.net/rfc/comparable The DateTime class offers this feature - it would be nice if this could be made usable for userland classes/objects, too. Best regards Christian Stoller -Original Message- From: p...@golemon.com [mailto:p...@golemon.com] On Behalf Of Sara Golemon Sent: Friday, November 09, 2012 1:07 AM To: PHP internals Subject: [PHP-DEV] Object comparison From: http://php.net/manual/en/language.operators.comparison.php An object compared to anything which is not a bool, null, or object should result in the object appearing to be greater than the other operand. For example: $a = new stdClass(); $b = new stdClass(); var_dump(null $a); var_dump(false $a); var_dump(true == $a); var_dump($a == $b); var_dump(0 $a); var_dump(1 $a); // false var_dump(2 $a); // false var_dump(foo $a); var_dump(2 $a); var_dump(tmpfile() $a); Based on docs, I expect all nine of these to yield true, however in practice, the two marked false come out as false because the RHS object is converted to an integer (1), contrary to the docs. Doc bug? Or code bug? I'm inclined to call it a code bug, but wanted others' thoughts. -Sara -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- github.com/KingCrunch
Re: [PHP-DEV] Object comparison
hi! On Fri, Nov 9, 2012 at 1:07 AM, Sara Golemon poll...@php.net wrote: From: http://php.net/manual/en/language.operators.comparison.php An object compared to anything which is not a bool, null, or object should result in the object appearing to be greater than the other operand. For example: $a = new stdClass(); $b = new stdClass(); var_dump(null $a); var_dump(false $a); var_dump(true == $a); var_dump($a == $b); var_dump(0 $a); var_dump(1 $a); // false var_dump(2 $a); // false var_dump(foo $a); var_dump(2 $a); var_dump(tmpfile() $a); Based on docs, I expect all nine of these to yield true, however in practice, the two marked false come out as false because the RHS object is converted to an integer (1), contrary to the docs. Doc bug? Or code bug? I'm inclined to call it a code bug, but wanted others' thoughts. As stated by other before, comparing scalars and objects sound wrong in the 1st place. But I have seen way too many weird codes relying on weird things :). That's why I would not be in favor of changing anything in this area as some code out there may rely on that (have the feeling that object comparison has been like that since 5.0). Cheers, -- Pierre @pierrejoye | http://blog.thepimp.net | http://www.libgd.org -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Object comparison
On Fri, Nov 9, 2012 at 2:18 PM, Christian Stoller stol...@leonex.de wrote: I would like to place a suggestion for comparing objects (I hope it is no problem, because this does not have anything to do with Sara's question - but it came to my mind when I read her mail). It would be a great feature if objects could be compared to other objects with , and the other operators, like it is suggested in RFC https://wiki.php.net/rfc/comparable The DateTime class offers this feature - it would be nice if this could be made usable for userland classes/objects, too. I really like that idea, and the patch looks pretty good and looks backward compatible. I'm +1 with it I'm also +1 with patching the doc to describe the full default compare behavior. Julien.Pauli -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Object comparison
On Fri, Nov 9, 2012 at 2:00 PM, jpauli jpa...@php.net wrote: On Fri, Nov 9, 2012 at 2:18 PM, Christian Stoller stol...@leonex.de wrote: I would like to place a suggestion for comparing objects (I hope it is no problem, because this does not have anything to do with Sara's question - but it came to my mind when I read her mail). It would be a great feature if objects could be compared to other objects with , and the other operators, like it is suggested in RFC https://wiki.php.net/rfc/comparable The DateTime class offers this feature - it would be nice if this could be made usable for userland classes/objects, too. I really like that idea, and the patch looks pretty good and looks backward compatible. I'm +1 with it I'm also +1 with patching the doc to describe the full default compare behavior. Another spot in the docs I would enjoy discussion about ,,=,= comparing two objects: http://php.net/manual/en/language.oop5.object-comparison.php I notice this behavior is suggested in a comment for use in an array_uintersect callback: http://php.tonnikala.org/manual/el/function.array-uintersect.php#72841 Indeed, trying to intersect two arrays of objects with a user callback 'works' with the comparison operator, but maybe this is just luck, or subject to BC break later? ?php class myclass { function __construct($name) { $this-name = $name; } } $o1 = new myclass('o1'); $o2 = new myclass('o2'); $o3 = new myclass('o3'); $o4 = new myclass('o4'); $a1 = array($o1, $o2, $o3); $a2 = array($o3, $o2, $o4); var_dump(array_uintersect($a1, $a2, function($v1, $v2) { if($v1 === $v2) return 0; if($v1 $v2) return 1; return -1; })); I'd expect the comparison there to be roughly equivalent to if(spl_object_hash($v1) spl_object_hash($v2)), no? Info on the default behavior here would be nice. -nathan
Re: [PHP-DEV] Object comparison
Hi! Doc bug? Or code bug? I'm inclined to call it a code bug, but wanted others' thoughts. I would say comparing object to a number makes little sense, so no reason to define any specific result there. It may be true, false or bologna sandwich. The docs say what happens when the first parameter is object, but say nothing what happens if the second one is object. This is for reason - if you compare object to array, they can't both be greater, something has to take priority. The docs say first arg takes priority. So by docs, comparison of (number, object) has no defined value, while comparison of (object, number) has. -- Stanislav Malyshev, Software Architect SugarCRM: http://www.sugarcrm.com/ (408)454-6900 ext. 227 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Object comparison
On Thu, Nov 8, 2012 at 7:07 PM, Sara Golemon poll...@php.net wrote: From: http://php.net/manual/en/language.operators.comparison.php An object compared to anything which is not a bool, null, or object should result in the object appearing to be greater than the other operand. For example: $a = new stdClass(); $b = new stdClass(); var_dump(null $a); var_dump(false $a); var_dump(true == $a); var_dump($a == $b); var_dump(0 $a); var_dump(1 $a); // false var_dump(2 $a); // false var_dump(foo $a); var_dump(2 $a); var_dump(tmpfile() $a); Based on docs, I expect all nine of these to yield true, however in practice, the two marked false come out as false because the RHS object is converted to an integer (1), contrary to the docs. Doc bug? Or code bug? I'm inclined to call it a code bug, but wanted others' thoughts. Hi Sara, I believe the documentation is failing us here. From what I see the docs are pretty lacking in what should be expected in terms of object comparison. For example, the page at http://php.net/types.comparisons doesn't even include objects in the comparison table. Of course by looking at the Comparison with Various Types table at http://php.net/language.operators.comparison one gets the impression that the Operand 1 and Operand 2 columns are to signify LVALUE and RVALUE operands, respectively. Obviously this isn't the case and the documentation just fails us here. var_dump(new stdclass 1, newstdcalss 1, 1 new stdclass, 1 new stdclass); // false, false, false, false Clearly there are cases when the object can be neither less-than nor greater-than an operand in a comparison. The statement object is always greater in that table is misleading and doesn't tell us the whole truth. For example, class foo { public function __toString() { return ''; } } var_dump(new foo '', new foo '', '' new foo, '' new foo); // false, false, false, false Obviously there are more complex cases where the object may not pan out to be greater than a non-object type that the documentation fails to address. -Sara -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Object comparison
André Luis Ferreira da Silva Bacci wrote: Hi, I was in a discuss about PHP's features vs Python's features these days and gat down in this BC: http://bugs.php.net/bug.php?id=33626 Does it ring any bell to anyone? []s André AE In PHP4 an object was a value type in PHP5 it is a handle and works like in Java - === compares only the handles. However, probably that behaviour needs its presence in the docs (if not already there). == works like a.equals(b) Andrey -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Object comparison
Andrey Hristov wrote: André Luis Ferreira da Silva Bacci wrote: Hi, I was in a discuss about PHP's features vs Python's features these days and gat down in this BC: http://bugs.php.net/bug.php?id=33626 Does it ring any bell to anyone? In PHP4 an object was a value type in PHP5 it is a handle and works like in Java - === compares only the handles. However, probably that behaviour needs its presence in the docs (if not already there). == works like a.equals(b) Hun... This will make the python guy happy (?)... Anyway, cc'ing to doc list and changing bug to doc problem. The docs state about same class, compare properties the same way as arrays, witch is plain wrong in PHP 5. I am translator, but my english is horrible ;) []s André AE -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Object comparison bug?
On Tue, 31 Aug 2004, Andi Gutmans wrote: Hi Christian, This was a backwards compatibility issue and therefore, we made sure that PHP 5 behaves the same way as PHP 4. So if both objects are PHP objects it will do a PHP 4 object comparison. If you use === (is identical) then we will only compare handles unless in zend1.compatibility_mode. This should give people the best of both worlds being able to choose comparison (==) or strict identical (===). Yup, and making this backward compatible was done after I wrote those slides ;-) Derick -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] Object comparison bug?
Hi Christian, This was a backwards compatibility issue and therefore, we made sure that PHP 5 behaves the same way as PHP 4. So if both objects are PHP objects it will do a PHP 4 object comparison. If you use === (is identical) then we will only compare handles unless in zend1.compatibility_mode. This should give people the best of both worlds being able to choose comparison (==) or strict identical (===). Andi At 02:17 PM 8/31/2004 +0200, Christian Stocker wrote: Hi The following came up in a bug report (http://bugs.php.net/?id=29911 , but it doesn't matter, as he tried something which doesn't work either way) ?php class foo {}; $foo1 = new foo(); $foo2 = new foo(); var_dump($foo1 == $foo2); ? prints now true, but according to Derick and http://talks.php.net/show/migrating-ffm/8 it should print false in PHP 5 Bug or expected behaviour? (I don't care much, since I wouldn't compare objects that way in the first place ;) ) chregu -- christian stocker | Bitflux GmbH | schoeneggstrasse 5 | ch-8004 zurich phone +41 1 240 56 70 | mobile +41 76 561 88 60 | fax +41 1 240 56 71 http://www.bitflux.ch | [EMAIL PROTECTED] | gnupg-keyid 0x5CE1DECB -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php