views:

40

answers:

2

Greetings,

I've got two instances of a PHP5 Class (say ClassA), and I want to compare to see whether they are equal. In C#, I can write a .equals(ClassA other) method for ClassA, which will be overloaded onto the == operator.

Is there an equivalent way in PHP5 to overload a method in a class and have that be used for == comparison, or am I forced to do something like $instanceA->equals($instanceB)?

Thanks! Alexey

+1  A: 

In PHP5 you cannot overload operators like that. Built-in classes (extensions in C) can define their own methods, but in PHP code there is nothing to be done. See also: http://www.php.net/manual/en/language.oop5.object-comparison.php

Wrikken
Yep, internal classes can override <code>compare_object</code>, but it will wonly work if both objects being compared have the same <code>compare_object</code> handler, otherwise the comparison will always yield false.
Artefacto
+3  A: 

Ah, sorry missed the point of the question about overloading. It does not look like that is possible as stated above. However, I did find a helpful example for implementing your own.

I found this link showing some example code: Comparable equals

Derek Litz