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:
<?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); ?> |
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:
<?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); ?> |
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:
<?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); ?> |
Geeks Object
(
[a] => 1
[b] => 2
[d] => 3
[e] => 4
[f] => 5
[g] => 6
)
Recommended Posts:
- ES6 | Merge Objects
- How to merge properties of two JavaScript objects dynamically?
- Extract unique objects by attribute from array of objects.
- PHP | Objects
- Objects in Javascript
- Bootstrap 4 | Media Objects
- Scala | Package Objects
- JavaScript | Date Objects
- Perl | Objects in OOPs
- Media Objects in Bootstrap with Examples
- Understanding Classes and Objects in Java
- Max/Min value of an attribute in an array of objects in JavaScript
- Sort array of objects by object fields in PHP
- How to remove Objects from Associative Array in JavaScript ?
- How to remove duplicates from an array of objects using JavaScript ?
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.


