如何合并两个对象?(复制)

时间:2022-06-06 09:13:39

Possible Duplicate:
What is the best method to merge two PHP objects?

可能的副本:合并两个PHP对象的最佳方法是什么?

I have an object, $foo, which has some methods and properties already defined and another object, $bar, which is just a set of properties. I want to merge the entirety of $bar into $foo such that all the properties of $bar become properties of $foo.

我有一个对象,$foo,它有一些已经定义的方法和属性,还有一个对象,$bar,它只是一组属性。我想把$bar的全部合并成$foo,这样$bar的所有属性都变成$foo的属性。

So if beforehand I had, $bar->foobar, afterwards I should be able to use $foo->foobar.

如果之前我有,$bar->foobar,之后我应该可以使用$foo->foobar。

At the moment, I am doing the following:

目前,我正在做以下工作:

foreach($bar as $key => $value) {
    $foo->$key = $value;
    }

However, if I were using arrays, I would just do one of the following:

但是,如果我使用数组,我只会做以下其中之一:

$foo += $bar;

$foo = array_merge($foo, $bar);

Is there a similar way of doing this with objects or am I doing it the right way already?

是否有类似的方法来处理对象或者我已经用正确的方法了?

Please note that I do not what $bar to itself become a property of $foo i.e. not $foo->bar->foobar

请注意,我不认为$bar本身成为$foo的属性,即不是$foo->bar->foobar

3 个解决方案

#1


3  

If $bar has all of $foo's properties, then it is a Foo. Thus, your Bar class should extend (inherit from) Foo. (Or vice versa, if I got your names confused!)

如果$bar拥有$foo的所有属性,那么它就是foo。因此,Bar类应该扩展(继承)Foo。(反之亦然,如果我把你的名字搞混了!)

If you don't want to use inheritance for some reason, you can keep one object inside the other, and redirect calls to the other's properties from the outer object dynamically via magic methods like __get() and __set (see here)

如果您不希望出于某种原因使用继承,那么可以将一个对象保存在另一个对象中,并通过__get()和__set(参见这里)等魔法方法动态地从外部对象重定向对另一个对象的属性的调用

#2


15  

Try this code

试试这个代码

echo $obj->name;  //show: carlos
echo $obj2->lastname; //show: montalvo here

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

$obj_merged->name; //show: carlos

$obj_merged->lastname; //show: montalvo here

#3


0  

There is no built-in function to copy all properties of one object to another one.

没有将一个对象的所有属性复制到另一个对象的内置函数。

The way you do it is probably the simplest. An alternative to provide the class of $foo with a __get method which returns the property directly from $bar.

你的方法可能是最简单的。提供$foo类的另一种方法是__get方法,该方法直接从$bar返回属性。

#1


3  

If $bar has all of $foo's properties, then it is a Foo. Thus, your Bar class should extend (inherit from) Foo. (Or vice versa, if I got your names confused!)

如果$bar拥有$foo的所有属性,那么它就是foo。因此,Bar类应该扩展(继承)Foo。(反之亦然,如果我把你的名字搞混了!)

If you don't want to use inheritance for some reason, you can keep one object inside the other, and redirect calls to the other's properties from the outer object dynamically via magic methods like __get() and __set (see here)

如果您不希望出于某种原因使用继承,那么可以将一个对象保存在另一个对象中,并通过__get()和__set(参见这里)等魔法方法动态地从外部对象重定向对另一个对象的属性的调用

#2


15  

Try this code

试试这个代码

echo $obj->name;  //show: carlos
echo $obj2->lastname; //show: montalvo here

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

$obj_merged->name; //show: carlos

$obj_merged->lastname; //show: montalvo here

#3


0  

There is no built-in function to copy all properties of one object to another one.

没有将一个对象的所有属性复制到另一个对象的内置函数。

The way you do it is probably the simplest. An alternative to provide the class of $foo with a __get method which returns the property directly from $bar.

你的方法可能是最简单的。提供$foo类的另一种方法是__get方法,该方法直接从$bar返回属性。