__eq__
私の夢の 1 つは、 php オブジェクトでpython リッチ比較 (のようなもの) を使用することです。
class A {
public $a = 1;
public function __eq__($other) {
return $this->a == $other->a;
}
}
class B {
public $a = 2;
public function __eq__($other) {
return $this->a == $other->a;
}
}
class C {
public $a = 1;
public function __eq__($other) {
return $this->a == $other->a;
}
}
$a = new A();
$b = new B();
$c = new C();
echo $a == $b; //false
echo $a == $c; //true
たとえば、データベースIDでモデル(オブジェクト)を高速比較するためのスマートなメカニズムが必要です。
PHPで何らかの方法で可能ですか?