Given two objects of same class, merge both objects into a single object.
class MyClass {}
$objectA = new MyClass();
$objectA->a = 1;
$objectA->b = 2;
$objectA->d = 3;
$objectB = new MyClass();
$objectB->d = 4;
$objectB->e = 5;
$objectB->f = 6;
// Using array_merge
$obj_merged = (object) array_merge((array) $objectA, (array) $objectB);
print_r($obj_merged);
stdClass Object
(
[a] => 1
[b] => 2
[d] => 4
[e] => 5
[f] => 6
)
// Using array union operator
$obj_union = (object)((array) $objectA +(array) $objectB);
print_r($obj_union);
stdClass Object
(
[a] => 1
[b] => 2
[d] => 3
[e] => 5
[f] => 6
)