Property values from later objects overwrite the previous objects.
class A {
public $a = 10;
public $b = 20;
};
class B {
public $c = 30;
public $d = 40;
};
* class C {
public $c = 35;
public $e = 50;
};
$obj1 = new A;
$obj2 = new B;
$obj3 = new C;
X::mergeObjects($obj1, $obj2, $obj3);
// object {'a': 10, 'b': 20, 'c': 35, 'd': 40, 'e': 50}
function(object $o1, object $o2)
{
$args = \func_get_args();
if (\count($args) > 2) {
for ($i = \count($args) - 1; $i > 1; $i--) {
if (!is_object($args[$i])) {
throw new Exception('The provided argument must be an object, ' . gettype($args[$i]) . ' given.');
}
$args[$i - 1] = self::mergeObjects($args[$i - 1], $args[$i]);
}
$o2 = $args[1];
}
$a1 = self::toArray($o1);
$a2 = self::toArray($o2);
$res = self::mergeArrays($a1, $a2);
return self::toObject($res);
}