I need to merge 2 custom objects of the same type. Here's what the class looks like:
<?php
class a{
public $one;
public $two;
public function doSomething(){
//do stuff
}
}
I have got 2 instance of a
that I need to merge together. I understand that I can use array_merge like this:
$result = (object)array_merge((array)$a1, (array)$a2);
But the problem is that I need the result to be of the a
class and not stdObj
.
If I do:
$result->doSomething()
an error results: PHP Fatal error: Call to undefined method stdClass::doSomething()
Since we cannot type cast to non-primitives, one cannot do: $result = (a)array_merge((array)$a1, (array)$a2);
Besides using a loop to iterate through one object and get and set values, are there more performant or neater ways to do this?