Add properties to stdClass object from another object

前端 未结 4 1813
悲哀的现实
悲哀的现实 2021-02-07 04:26

I would like to be able to do the following:

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

$obj2 = new stdClass;
$obj2->message = \"OK\";
4条回答
  •  攒了一身酷
    2021-02-07 04:41

    This is more along the lines of they way that you didn't want to do it....

    $extended = (object) array_merge((array)$obj, (array)$obj2);
    

    However I think that would be a little better than having to iterate over the properties.

提交回复
热议问题