How to merge two PHP objects?

Given two objects of same class and the task is to merge both objects into single object.

Approach 1: Convert object into data array and merge them using array_merge() function and convert this merged array back into object of class stdClass.

Note: While merging the objects using array_merge(), elements of array in argument1 are overwritten by elements of array in argument2. This may nullify the resulting elements in final object if array in argument2 has null values.

Note: Functions are not copied using this approach. Only use this approach if class only contain variables.

Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP prgoram to merge two objects
  
class Geeks {
    // Empty class
}
  
$objectA = new Geeks();
$objectA->a = 1;
$objectA->b = 2;
$objectA->d = 3;
  
$objectB = new Geeks();
$objectB->d = 4;
$objectB->e = 5;
$objectB->f = 6;
  
$obj_merged = (object) array_merge(
        (array) $objectA, (array) $objectB);
          
print_r($obj_merged);
  
?>

chevron_right


Output:

stdClass Object
(
    [a] => 1
    [b] => 2
    [d] => 4
    [e] => 5
    [f] => 6
)

Approach 2: Merge the object using array_merge() method and convert this merged array to object using convertObjectClass function. This function is used to convert object of the initial class into serialized data using serialize() method. Unserialize the serialized data into instance of the final class using unserialize() method. Using this approach obtain an object of user defined class Geeks rather the standard class stdClass.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP prgoram to merge two objects
  
class Geeks {
    // Empty class
}
  
$objectA = new Geeks();
$objectA->a = 1;
$objectA->b = 2;
$objectA->d = 3;
  
$objectB = new Geeks();
$objectB->d = 4;
$objectB->e = 5;
$objectB->f = 6;
  
// Function to convert class of given object
function convertObjectClass($array, $final_class) {
    return unserialize(sprintf(
        'O:%d:"%s"%s',
        strlen($final_class),
        $final_class,
        strstr(serialize($array), ':')
    ));
}
  
$obj_merged = convertObjectClass(array_merge(
        (array) $objectA, (array) $objectB), 'Geeks');
  
print_r($obj_merged);
  
?>

chevron_right


Output:

Geeks Object
(
    [a] => 1
    [b] => 2
    [d] => 4
    [e] => 5
    [f] => 6
)

Approach 3: Create a new object of the original class and assign all the properties of both objects to this new object by using foreach loop. This is a simple and clean approach of merging two objects.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP prgoram to merge two objects
  
class Geeks {
    // Empty class
}
  
$objectA = new Geeks();
$objectA->a = 1;
$objectA->b = 2;
$objectA->d = 3;
  
$objectB = new Geeks();
$objectB->e = 4;
$objectB->f = 5;
$objectB->g = 6;
  
// Function to convert class of given object
function convertObjectClass($objectA,
                $objectB, $final_class) {
  
    $new_object = new $final_class();
  
    // Initializing class properties
    foreach($objectA as $property => $value) {
        $new_object->$property = $value;
    }
      
    foreach($objectB as $property => $value) {
        $new_object->$property = $value;
    }
  
    return $new_object;
}
  
$obj_merged = convertObjectClass($objectA
                        $objectB, 'Geeks');
                          
print_r($obj_merged);
  
?>

chevron_right


Output:

Geeks Object
(
    [a] => 1
    [b] => 2
    [d] => 3
    [e] => 4
    [f] => 5
    [g] => 6
)


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.