/** * @param mixed $obj1 * @param mixed $obj2 * @return boolean * @throws \Exception */ public static function areEquals($obj1, $obj2) { if (gettype($obj1) != gettype($obj2)) { return false; } switch (gettype($obj1)) { case "boolean": return $obj1 === $obj2; break; case "integer": case "double": case "string": return $obj1 == $obj2; break; case "array": return count(array_diff($obj1, $obj2)) == 0; break; case "object": // Do nothing break; case "NULL": return true; break; default: throw new \Exception('Types like "resource" and "unknown type" are incomparable.'); break; } if (get_class($obj1) != get_class($obj2)) { return false; } $reflectObj1 = new \ReflectionClass($obj1); $reflectObj2 = new \ReflectionClass($obj2); /* @var $propertiesThis \ReflectionProperty[] */ $propertiesObj1 = $reflectObj1->getProperties(); /* @var $propertiesObj \ReflectionProperty[] */ $propertiesObj2 = $reflectObj2->getProperties(); if (count($propertiesObj1) != count($propertiesObj2)) { return false; } for ($i = 0; $i < count($obj1); $i++) { /* @var $propertyObj1 \ReflectionProperty */ $propertyObj1 = $propertiesObj1[$i]; /* @var $propertyObj2 \ReflectionProperty */ $propertyObj2 = $propertiesObj2[$i]; $propertyObj1->setAccessible(true); $propertyObj2->setAccessible(true); $areEquals = MObject::areEquals($propertyObj1->getValue($obj1), $propertyObj2->getValue($obj2)); if ($areEquals === false) { return false; } } return true; }