Airmanbzh
Airmanbzh

Reputation: 647

Overloading method of comparison for custom class

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

Upvotes: 1

Views: 1126

Answers (1)

Ray
Ray

Reputation: 41428

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

Upvotes: 1

Related Questions