启用“.equals” PHP5 类的等效项
我有两个 PHP5 类实例(例如 ClassA),我想比较它们是否相等。在 C# 中,我可以为 ClassA 编写一个 .equals(ClassA other) 方法,该方法将重载到 == 运算符上。
PHP5 中是否有等效的方法来重载类中的方法并将其用于 == 比较,或者我是否被迫执行 $instanceA->equals($instanceB) 之类的操作?
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)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
啊,抱歉错过了有关超载的问题的重点。如上所述,这看起来不可能。不过,我确实找到了一个有用的示例来实现您自己的示例。
我发现此链接显示了一些示例代码:比较等于
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
在 PHP5 中你不能重载这样的运算符。内置类(C 中的扩展)可以定义自己的方法,但在 PHP 代码中无需执行任何操作。另请参阅:http://www.php.net/manual /en/language.oop5.object-comparison.php
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