从另一个对象向stdClass对象添加属性

时间:2022-09-24 18:52:14

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以使其包含$ obj2的属性,例如:

$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

extend($ obj,$ obj2); //将$ obj2中的所有属性添加到$ obj

Thanks!

4 个解决方案

#1


23  

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.

但是我认为这比迭代属性要好一些。

#2


8  

if the object is the instance of stdClass (that's in your case) you can simply extend your object like...

如果对象是stdClass的实例(在您的情况下),您可以简单地扩展您的对象,如...

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

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

$obj->message = $message;
$obj->subject = $subject;

.... and as many as you wish.

....和你想要的一样多。

#3


5  

You could use get_object_vars() on one of the stdClass object, iterate through those, and add them to the other:

您可以在其中一个stdClass对象上使用get_object_vars(),遍历这些对象,并将它们添加到另一个:

function extend($obj, $obj2) {
    $vars = get_object_vars($obj2);
    foreach ($vars as $var => $value) {
        $obj->$var = $value;
    }
    return $obj;
}

Not sure if you'd deem that more elegant, mind you.

请注意,不确定你是否认为更优雅。

Edit: If you're not stingy about actually storing them in the same place, take a look at this answer to a very similar question.

编辑:如果你并不吝啬实际存储它们在同一个地方,请看一个非常相似的问题的答案。

#4


1  

have a look at object cloning http://php.net/manual/en/language.oop5.cloning.php

看一下对象克隆http://php.net/manual/en/language.oop5.cloning.php

#1


23  

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.

但是我认为这比迭代属性要好一些。

#2


8  

if the object is the instance of stdClass (that's in your case) you can simply extend your object like...

如果对象是stdClass的实例(在您的情况下),您可以简单地扩展您的对象,如...

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

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

$obj->message = $message;
$obj->subject = $subject;

.... and as many as you wish.

....和你想要的一样多。

#3


5  

You could use get_object_vars() on one of the stdClass object, iterate through those, and add them to the other:

您可以在其中一个stdClass对象上使用get_object_vars(),遍历这些对象,并将它们添加到另一个:

function extend($obj, $obj2) {
    $vars = get_object_vars($obj2);
    foreach ($vars as $var => $value) {
        $obj->$var = $value;
    }
    return $obj;
}

Not sure if you'd deem that more elegant, mind you.

请注意,不确定你是否认为更优雅。

Edit: If you're not stingy about actually storing them in the same place, take a look at this answer to a very similar question.

编辑:如果你并不吝啬实际存储它们在同一个地方,请看一个非常相似的问题的答案。

#4


1  

have a look at object cloning http://php.net/manual/en/language.oop5.cloning.php

看一下对象克隆http://php.net/manual/en/language.oop5.cloning.php