Reputation: 6395
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)?
Upvotes: 0
Views: 1813
Reputation: 10897
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
Upvotes: 4
Reputation: 70490
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
Upvotes: 1