Add properties to stdClass object from another object

Posted by Florin on Stack Overflow See other posts from Stack Overflow or by Florin
Published on 2010-04-16T11:55:10Z Indexed on 2010/04/16 12:03 UTC
Read the original article Hit count: 401

Filed under:
|
|
|
|

I would like to be able to do the following:

$obj = new stdClass;
$obj->status = "success";

$obj2 = new stdClass;
$obj2->message = "OK";

How can I extend $obj so that it contains the properties of $obj2, eg:

$obj->status //"success"

$obj->message // "OK"

I know I could use an array, add all properties to the array and then cast that back to object, but is there a more elegant way, something like this:

extend($obj, $obj2); //adds all properties from $obj2 to $obj

Thanks!

© Stack Overflow or respective owner

Related posts about php

Related posts about stdclass