自定义类的比较重载方法 [英] Overloading method of comparison for custom class

查看:81
本文介绍了自定义类的比较重载方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重载个人类的比较方法.

I want to overload methods of comparison for a personnal class.

例如,如果我这样写:$ object1< $ object2 PHP将使用此功能:

For example if I write this : $object1 < $object2 Php will use this function :

function compare($a, $b){
    if($a->attribute == $b->attribute){return 0;}
    else{return $a->attribute > $b->attribute ? 1 : -1;}
}

有没有办法做到这一点?

Is there a way to do this ?

我已经看过,但是我不能使用这些解决方案

I already seen this and this but I can't use these solutions

推荐答案

您上面提到的PECL解决方案是您唯一的选择. PHP不提供其他语言提供的运算符重载.

The PECL solution you point to above is your only option. PHP does not provide operator overloading as available in other languages.

这篇关于自定义类的比较重载方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆