User Contributed Notes 12 notes

Note that when comparing object attributes, the comparison is recursive (at least, it is with PHP 5.2). That is, if $a->x contains an object then that will be compared with $b->x in the same manner. Be aware that this can lead to recursion errors:<?phpclass Foo { public $x;}$a = new Foo();$b = new Foo();$a->x = $b;$b->x = $a;

PHP and MySQL both provide ways to sort your data already, and it is a good idea to use that if possible. However, since this section is on comparing your own PHP objects (and that you may need to alter the sorting method in PHP), here is an example of how you can do that using PHP's "user-defined" sort method, usort() and your own class compare() methods.

<?php

/* * Employee.php * * This class defines a compare() method, which tells PHP the sorting rules * for this object - which is to sort by emp_id. * */class Employee{ public $first; public $last; public $emp_id; // the property we're interested in...

Important Note: Your PHP code will never directly call the Employee's compare() method, but PHP's usort() calls it many many times. Also, when defining the rules for sorting, make sure to get to a "primitive type" level... that is, down to a number or string, and that the function returns a -1, 0, or 1, for reliable and consistent results.

In response to "rune at zedeler dot dk"s comment about class contents being equal, I have a similar issue. I want to sort an array of objects using sort().

I know I can do it with usort(), but I'm used to C++ where you can define operators that allow comparison. I see in the zend source code that it calls a compare_objects function, but I don't see any way to implement that function for an object. Would it have to be an extension to provide that interface?

If so, I'd like to suggest that you allow equivalence and/or comparison operations to be defined in a class definition in PHP. Then, the sorts of things rune and I want to do would be much easier.

Even though $link1 and $link2 contain different Leaf objects, they are still equivalent because the Leaf objects are themselves equivalent.

The practical upshot is that using "==" when "===" would be more appropriate can result in a severe performance penalty, especially if the objects are large and/or complex. In fact, if there are any circular relationships involved between the objects or (recursively) any of their properties, then a fatal error can result because of the implied infinite loop.

So preference should be given to comparing objects with "===" rather than "=="; if two distinct objects are to be compared for equivalence, try to do so by examining suitable individual properties. (Maybe PHP could get a magic "__equals" method that gets used to evaluate "=="? :) )

I haven't found a build-in function to check whether two obects are identical - that is, all their fields are identical.In other words,

<?class A { var $x; function __construct($x) { $this->x = $x; }

}$identical1 = new A(42);$identical2 = new A(42);$different = new A('42');?>

Comparing the objects with "==" will claim that all three of them are equal. Comparing with "===" will claim that all are un-equal.I have found no build-in function to check that the two identicals are identical, but not identical to the different.

This example is way too much confusing, if you new to php comparison motor, you should think (after reading this example) that '==' is actually comparing the type of the objects. that's not true, it actually compares the type of the objects AND the properties of them.