将 stdClass 对象转换/转换为另一个类
我正在使用第三方存储系统,无论我出于某种不明原因输入什么,它都只返回 stdClass 对象。所以我很好奇是否有一种方法可以将 stdClass 对象转换/转换为给定类型的完整对象。
例如,以下内容:
//$stdClass is an stdClass instance
$converted = (BusinessClass) $stdClass;
我只是将 stdClass 转换为数组并将其提供给 BusinessClass 构造函数,但也许有一种方法可以恢复我不知道的初始类。
注意:我对“更改存储系统”类型的答案不感兴趣,因为它不是兴趣点。请更多地将其视为关于语言能力的学术问题。
干杯
I'm using a third party storage system that only returns me stdClass objects no matter what I feed in for some obscure reason. So I'm curious to know if there is a way to cast/convert an stdClass object into a full fledged object of a given type.
For instance something along the lines of:
//$stdClass is an stdClass instance
$converted = (BusinessClass) $stdClass;
I am just casting the stdClass into an array and feed it to the BusinessClass constructor, but maybe there is a way to restore the initial class that I am not aware of.
Note: I am not interested in 'Change your storage system' type of answers since it is not the point of interest. Please consider it more an academic question on the language capacities.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
请参阅类型杂耍手册了解可能的转换。
允许的转换为:
您必须 编写一个 Mapper 来执行从 stdClass 到另一个具体类的转换。应该不会太难做。
或者,如果您心情不好,可以修改以下代码:
将数组伪转换为某个类的对象。其工作原理是首先序列化数组,然后更改序列化的数据,使其代表某个类。然后结果被反序列化为此类的实例。但就像我说的,它是黑客式的,所以要预料到会有副作用。
对于对象到对象,代码是
See the manual on Type Juggling on possible casts.
The casts allowed are:
You would have to write a Mapper that does the casting from stdClass to another concrete class. Shouldn't be too hard to do.
Or, if you are in a hackish mood, you could adapt the following code:
which pseudocasts an array to an object of a certain class. This works by first serializing the array and then changing the serialized data so that it represents a certain class. The result is unserialized to an instance of this class then. But like I said, it's hackish, so expect side-effects.
For object to object, the code would be
您可以使用上述函数来转换不相似的类对象(PHP >= 5.3)
示例:
You can use above function for casting not similar class objects (PHP >= 5.3)
EXAMPLE:
将
stdClass
的所有现有属性移动到指定类名的新对象:用法:
输出:
由于
new
运算符的存在,这会受到限制,因为不知道哪些参数它需要。对于你的情况可能很合适。To move all existing properties of a
stdClass
to a new object of a specified class name:Usage:
Output:
This is limited because of the
new
operator as it is unknown which parameters it would need. For your case probably fitting.我有一个非常相似的问题。简化的反射解决方案对我来说效果很好:
I have a very similar problem. Simplified reflection solution worked just fine for me:
希望有人觉得这很有用
Hope that somebody find this useful
更改了深度铸造的函数(使用递归)
Changed function for deep casting (using recursion)
考虑向 BusinessClass 添加一个新方法:
然后您可以从 $stdClass 创建一个新的 BusinessClass:
consider adding a new method to BusinessClass:
then you can make a new BusinessClass from $stdClass:
还有另一种方法使用装饰器模式和 PHP 的 magic getter & 。设置器:
And yet another approach using the decorator pattern and PHPs magic getter & setters:
还有另一种方法。
由于最近的 PHP 7 版本,以下内容现在成为可能。
在此示例中,$foo 被初始化为匿名类,该类将一个数组或 stdClass 作为构造函数的唯一参数。
最终,我们循环遍历传递的对象中包含的每个项目,然后动态分配给对象的属性。
为了使这个 approch 事件更加通用,您可以编写一个接口或一个 Trait,您将在任何您希望能够强制转换 stdClass 的类中实现它们。
Yet another approach.
The following is now possible thanks to the recent PHP 7 version.
In this example, $foo is being initialized as an anonymous class that takes one array or stdClass as only parameter for the constructor.
Eventually, we loop through the each items contained in the passed object and dynamically assign then to an object's property.
To make this approch event more generic, you can write an interface or a Trait that you will implement in any class where you want to be able to cast an stdClass.
顺便说一句:如果您进行序列化,转换非常重要,主要是因为反序列化会破坏对象的类型并变成 stdclass,包括 DateTime 对象。
我更新了@Jadrovski 的示例,现在它允许对象和数组。
示例
数组
代码:(其递归)。但是,我不知道它是否与数组递归。可能是缺少一个额外的 is_array
BTW: Converting is highly important if you are serialized, mainly because the de-serialization breaks the type of objects and turns into stdclass, including DateTime objects.
I updated the example of @Jadrovski, now it allows objects and arrays.
example
example array
code: (its recursive). However, i don't know if its recursive with arrays. May be its missing an extra is_array
将其转换为数组,返回该数组的第一个元素,并将返回参数设置为该类。现在您应该获得该类的自动完成功能,因为它会将其重新识别为该类而不是 stdclass。
Convert it to an array, return the first element of that array, and set the return param to that class. Now you should get the autocomplete for that class as it will regconize it as that class instead of stdclass.