Overloading method of comparison for custom class

流过昼夜 提交于 2019-12-02 12:57:52

问题


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

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


回答1:


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



来源:https://stackoverflow.com/questions/10600460/overloading-method-of-comparison-for-custom-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!