合并两个PHP对象的最佳方法是什么? - What is the best method to merge two PHP objects?

10/14 18:45
阅读数 0

问题:

We have two PHP5 objects and would like to merge the content of one into the second. 我们有两个PHP5对象,并希望将一个内容合并到第二个。 There are no notion of subclasses between them so the solutions described in the following topic cannot apply. 它们之间没有子类概念,因此以下主题中描述的解决方案无法应用。

How do you copy a PHP object into a different object type 如何将PHP对象复制到不同的对象类型中

//We have this:
$objectA->a;
$objectA->b;
$objectB->c;
$objectB->d;

//We want the easiest way to get:
$objectC->a;
$objectC->b;
$objectC->c;
$objectC->d;

Remarks: 备注:

  • These are objects, not classes. 这些是对象,而不是类。
  • The objects contain quite a lot of fields so a foreach would be quite slow. 这些对象包含很多字段,所以foreach会很慢。
  • So far we consider transforming objects A and B into arrays then merging them using array_merge() before re-transforming into an object but we can't say we are proud if this. 到目前为止,我们考虑将对象A和B转换为数组,然后在重新转换为对象之前使用array_merge()合并它们,但我们不能说如果这样我们感到自豪。

解决方案:

参考一: https://en.stackoom.com/question/1uY0
参考二: https://stackoom.com/question/1uY0
展开阅读全文
php
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部