Comparing two objects except pdo var

for my php project, I created group objects that need to be compared. Since they have a PDO connection stored in a variable, the == operator will always return false, because one variable is not equal. Is there any way to compare these objects other than comparing this unique PDO variable? The only way I could imagine was a huge loop that checks every variable. I would be very happy if someone knew a smarter way.

+4
source share
2 answers

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")); // true
var_dump(areSameExcept($a, $c, "pdo")); // false
+2
source

. .

, , , PDO.

, , . , , , , .

class GroupObj {
    public $prop1;
    public $prop2;
    public $prop3;
    public $db;

    public function __construct($prop1 = "", $prop2 = "", $prop3 = "") {
        $this->db = "connection established here";
        $this->prop1 = $prop1;
        $this->prop2 = $prop2;
        $this->prop3 = $prop3;
    }

    public function equalsTedious($object) {
        return ($this->prop1 == $object->prop1 &&
                $this->prop2 == $object->prop2 &&
                $this->prop3 == $object->prop3);
    }

    public function equals($object) {
        $result = true;
        foreach ($this as $key => $value) {
            # skip properties you don't want to compare
            if ($key == "db") {
                continue;
            }

            if ($this->{$key} != $object->{$key}) {
                $result = false;
                break;
            }
        }

        return $result;
    }
}

$group1 = new GroupObj(1, 2, 3);
$group2 = new GroupObj(1, 2, 3);
$group3 = new GroupObj(4, 5, 6);

if ($group1->equalsTedious($group2)) {
    echo "Equal but annoying to maintain 1.<br>";
}

if ($group1->equalsTedious($group3)) {
    echo "Equal but annoying to maintain 2.<br>";
}

if ($group1->equals($group2)) {
    echo "Equal with the loop version too 1.<br>";
}

if ($group1->equals($group3)) {
    echo "Equal with the loop version too 2.<br>";
}
+1

Source: https://habr.com/ru/post/1683849/


All Articles