What is the best method to merge two PHP objects?

merge two objects php laravel
php merge objects recursive
php merge two object arrays
php array merge
how to merge two array in php without using function
merge 2 arrays
php object to array
php merge multidimensional array

We have two PHP5 objects and would like to merge the content of one into the second. There are no notion of subclasses between them so the solutions described in the following topic cannot apply.

How do you copy a PHP object into a different object type

//We have this:
$objectA->a;
$objectA->b;
$objectB->c;
$objectB->d;

//We want the easiest way to get:
$objectC->a;
$objectC->b;
$objectC->c;
$objectC->d;

Remarks:

  • These are objects, not classes.
  • The objects contain quite a lot of fields so a foreach would be quite slow.
  • So far we consider transforming objects A and B into arrays then merging them using array_merge() before re-transforming into an object but we can't say we are proud if this.

If your objects only contain fields (no methods), this works:

$obj_merged = (object) array_merge((array) $obj1, (array) $obj2);

This actually also works when objects have methods. (tested with PHP 5.3 and 5.6)

If your objects only contain fields (no methods), this works: $obj_merged = (object​) array_merge((array) $obj1, (array) $obj2);. This actually also works when  The best way to merge two or more arrays in PHP is to use the array_merge () function. Items of arrays will be merged together, and values with the same string keys will be overwritten with the last value: To remove array values from another array (or arrays), use array_diff () .

You could create another object that dispatches calls to magic methods to the underlying objects. Here's how you'd handle __get, but to get it working fully you'd have to override all the relevant magic methods. You'll probably find syntax errors since I just entered it off the top of my head.

class Compositor {
  private $obj_a;
  private $obj_b;

  public function __construct($obj_a, $obj_b) {
    $this->obj_a = $obj_a;
    $this->obj_b = $obj_b;
  }

  public function __get($attrib_name) {
    if ($this->obj_a->$attrib_name) {
       return $this->obj_a->$attrib_name;
    } else {
       return $this->obj_b->$attrib_name;
    }
  }
}

Good luck.

How do you copy a PHP object into a different object type //We have this: $​objectA->a; $objectA->b; $objectB->c; $objectB->d; //We want the easiest way to get:  The array_merge() function merges one or more arrays into one array. Tip: You can assign one array to the function, or as many as you like. Note: If two or more array elements have the same key, the last one overrides the others.

foreach($objectA as $k => $v) $objectB->$k = $v;

I know there is array merge but how do you do it for objects? I have one object with all my user info and a second object with the users meta  Instantiate/create your object. Classes are the blueprints/templates of php objects. Classes don’t actually become objects until you do something called: instantiation. When you instantiate a class, you create an instance of it … thus creating the object. In other words, instantiation is the process of creating an instance of an object in memory. What memory?

If your objects only contain fields (no methods), this works: $obj_merged = (object​) array_merge((array) $obj1, (array) $obj2);. This actually also works when  providing a object as a array index doesn't try to us __toString() method so some volatile object identifier is used to index the array, which is breaking any persistency. Type hinting solves that, but while other than "string" type hinting doesn't work on ob jects, the automatic conversion to string should be very intuitive.

A very simple solution considering you have object A and B:

foreach($objB AS $var=>$value){
    $objA->$var = $value;
}

That's all. You now have objA with all values from objB.

Merges the elements of one or more arrays together so that the values of one are keys matter, this behaviour could be useful, and better than array_merge. $c = array_merge($a, $b); // shows a PHP warning: Argument #2 is not an array or array_merge_recursive functions will function correctly if non-array objects are  The __construct() method is called automatically by PHP when it sees the 'new' operator. Our __construct() method above requires the first and last names to be passed in as arguments and uses them to INITIALIZE objects when INSTANTIATING new INSTANCES.

What is the best method to merge two PHP objects? - Stack Overflow. 27 May 2009 What is the best method to merge two PHP objects? The way it works is you first combine the 2 meshes into one object using the Object Manager connect command. You then do loop selections on the mesh where you want it joined in edge mode, select the Stitch& Sew tool and hold down the shift key, drag between the 2 loops.

Get code examples like "merge two objects in php" instantly right from how to solve php mysqli_query function problem does not execute  Sometimes it will be great to have handy utility ready which combines two or multiple JSONObjects. Here is simple Java example which shows the same.

PHP provides a perfectly good mail() function, so initially this would probably be on entirely different sets of files, the task of merging two or more development  Given two sorted arrays, the task is to merge them in a sorted manner. Create an array arr3 [] of size n1 + n2. Traverse arr2 [] and one by one insert elements (like insertion sort) of arr3 [] to arr1 []. This step take O (n1 * n2) time. We have discussed implementation of above method in Merge two sorted arrays with O (1) extra space.

Comments
  • "The objects contain quite a lot of fields so a foreach would be quite slow." - Computers are pretty fast, 'quite slow' is often fast enough.
  • You may also use array_merge_recursive to have a deep copy behavior. You may also be interested in array_replace_recursive. The differences are explained in detail here: brian.serveblog.net/2011/07/31/php-array_replace-vs-array_merge
  • The object resulting from this will be an instance of stdclass. While it does "work" in a sense on objects with methods, it effectively ruins the object in that case (by removing the methods).
  • This is useful for returning multiple resultsets in a single function (And return just an object with key-value pairs.)
  • This will not work if there is an integer key in the object. Consider the following example : $arr1 = array('a' => 9, 'b' => 'asd'); $arr2 = array('a' => 10, 'd' => 'qwert', 0 => 100, 1 => 200, 4 => 400); $arr3 = array_merge($arr1, $arr2); echo(print_r($arr3, 1)); Actual Output : Array ( [a] => 10 [b] => asd [d] => qwert [0] => 100 [1] => 200 [2] => 400 ) Desired Output : Array ( [a] => 10 [b] => asd [d] => qwert [0] => 100 [1] => 200 [4] => 400 )
  • Is it just me or is this answer a verbatim copy of an answer that had already been posted for months? stackoverflow.com/a/794356/151509
  • Complete implementation would probably need __isset(), __unset() and implement Interator interface.
  • @porneL: what is Interator Interface?
  • I'd edit his comment, but you can't do that. I think he means Iterator
  • I like very much your solution, Allain, but I'm afraid it means we have to rewrite our entire application if we decide to use it.
  • Ok... then pick the way that doesn't require a complete rewrite.