I need to check if two PHP objects are equal in terms of equal values. Of course, I could easily add a method isEqualTo(...)to a class that compares all the relevant values. However, the specific class will change in the near future, and I would like to know if there is any automated way to do this.
Example:
class Contact {
private name;
private phone;
private someObject;
public function getName() {
return $this->name;
}
public function setName($newName) {
$this->name = $newName;
}
public function getPhone() {
return $this->phone;
}
public function setPhone($newPhone) {
$this->phone = $newPhone;
}
public function getSomeObject() {
return $this->someObject;
}
public function setSomeObject($newObj) {
$this->someObject = $newObj;
}
public function isEqualTo($contact) {
result = $this->name == $contact->getName();
result &= $this->phone == $contact->getPhone();
result &= $this->someObject == $contact->getSomeObject();
return result;
}
}
This will obviously work. Of course, I know about the restriction of comparison someObject(you need a specific object to be true), but this is normal.
However, the class Contactwill be expanded in the near future. Every time I add new properties / values to the class, I have to add them to isEqualTo. There is nothing complicated, but a little cumbersome.
, isEqualTo ?
get_class_vars get_object_vars, , vars, .
get_class_methodes, , . get... set... , , , " " .
: "" PHP ?