src/Headzoo/Core/Objects.php
public static function merge($obj, $obj2)
    {
        if (!is_object($obj) || !is_object($obj2)) {
            self::toss(
                "InvalidArgument",
                "Merged values must be objects. Got type {0}.",
                gettype($obj)
            );
        }
        
        if (func_num_args() > 2) {
            $args = func_get_args();
            $obj  = array_shift($args);
            foreach($args as $arg) {
                self::merge($obj, $arg);
            }
        } else {
            foreach($obj2 as $name => $value) {
                if (null !== $value) {
                    $obj->$name = $value;
                }
            }
        }
        
        return $obj;
    }
public static function merge($obj, $obj2)
    {
        if (!is_object($obj) || !is_object($obj2)) {
            self::toss(
                "InvalidArgument",
                "Merged values must be objects. Got type {0}.",
                gettype($obj)
            );
        }
        
        if (func_num_args() > 2) {
            $args = func_get_args();
            $obj  = array_shift($args);
            foreach($args as $arg) {
                self::merge($obj, $arg);
            }
        } else {
            foreach($obj2 as $name => $value) {
                if (null !== $value) {
                    $obj->$name = $value;
                }
            }
        }
        
        return $obj;
    }
public static function merge ( $ obj , $ obj2 ) { if ( ! is_object ( $ obj ) || ! is_object ( $ obj2 ) ) { self :: toss ( "InvalidArgument" , "Merged values must be objects. Got type {0}." , gettype ( $ obj ) ) ; } if ( func_num_args ( ) > 2 ) { $ args = func_get_args ( ) ; $ obj = array_shift ( $ args ) ; foreach ( $ args as $ arg ) { self :: merge ( $ obj , $ arg ) ; } } else { foreach ( $ obj2 as $ name => $ value ) { if ( null !== $ value ) { $ obj -> $ name = $ value ; } } } return $ obj ; }
Merges two or more objects

Copies the public property values from $obj2 to $obj1, and returns $obj1. Properties from $obj2
are either added to $obj1, or overwrite existing properties with the same name. Null values
do not overwrite non-null values. More than two objects may be merged.

Protected and private properties are not merged.

Examples:
```php
$obj_a = new stdClass();
$obj_a->job = "Circus Freak";

$obj_b = new stdClass();
$obj_b->name = "Joe"
$obj_b->job  = "Space Buccaneer";

$obj_c = new stdClass();
$obj_c->age = 23;

$merged = Objects::merge($obj_a, $obj_b, $obj_c);
var_dump($obj_a === $merged);
var_dump($merged);

// Outputs:
// bool(true);
// class stdClass {
//   public $name => string(4) "Joe"
//   public $job  => string(15) "Space Buccaneer"
//   public $age  => int(23)
//
// }

// The objects do not have to be the same type.
$obj_a = new Strings();
$obj_a->value = "PhpStorm Rocks!";

$obj_b = new Strings();
$obj_b->value = "So does IntelliJ IDEA!";

$obj_c = new stdClass();
$obj_c->website = "http://www.jetbrains.com/";

$merged = Objects::merge($obj_a, $obj_b, $obj_c);
var_dump($merged === $obj_a);
var_dump($merged);

// Outputs:
// bool(true)
// class Strings {
//   public $value   => string(22) "So does IntelliJ IDEA!",
//   public $website => string(25) "http://www.jetbrains.com/"
// }

// Easily merge objects into a new "blank" object.
$obj_a = new Strings();
$obj_a->value = "PhpStorm Rocks!";

$obj_b = new Arrays();
$obj_b->count = 23;

$merged = Objects::merge(new stdClass(), $obj_a, $obj_b);
var_dump($merged);

// Outputs:
// class stdClass {
//   public $value => string(15) "PhpStorm Rocks!",
//   public $count => 23
// }
```

@param  object $obj  The base object
@param  object $obj2 Gets merged into the base object
@param  object ...   Additional objects to merge
@return mixed
Merges two or more objects