Use this feature. It will use Reflection to compare each property except$exceptParameter
<?php
class Test
{
public $var1;
public $pdo;
public function __construct($var1, $pdo)
{
$this->var1 = $var1;
$this->pdo = $pdo;
}
}
$a = new Test("test1", "test2");
$b = new Test("test1", "test3");
$c = new Test("test2", "test4");
function areSameExcept($obj1, $obj2, $exceptParameter) {
$ref1 = new ReflectionClass($obj1);
$ref2 = new ReflectionClass($obj2);
$propertiesObj1 = $ref1->getProperties();
foreach ($propertiesObj1 as $propertyObj1) {
if ($propertyObj1->getName() === $exceptParameter) continue;
$propertyObj1->setAccessible(true);
$valueObj1 = $propertyObj1->getValue($obj1);
$propertyObj2 = $ref2->getProperty($propertyObj1->getName());
$propertyObj2->setAccessible(true);
$valueObj2 = $propertyObj2->getValue($obj2);
if ($valueObj1 !== $valueObj2) {
return false;
}
}
return true;
}
var_dump(areSameExcept($a, $b, "pdo"));
var_dump(areSameExcept($a, $c, "pdo"));
source
share