Overloading method of comparison for custom class

2019-02-28 22:37发布

问题:

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.