开发者

Enabling a '.equals' equivalent for PHP5 Classes

开发者 https://www.devze.com 2023-01-09 07:30 出处:网络
I\'ve got two instances of a PHP5 Class (say ClassA), and I w开发者_如何学Goant to compare to see whether they are equal.In C#, I can write a .equals(ClassA other) method for ClassA, which will be ove

I've got two instances of a PHP5 Class (say ClassA), and I w开发者_如何学Goant 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)?


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号