在 PHP 中获取对象依赖关系
我有一个依赖于其他对象的对象实例,例如
$objA = new Some_Class();
$objB = new Other_Class();
$objC = new Another_One();
$objA->property = new stdClass;
$objB->key = $objA;
$objB->arr = array(new Other_Object());
$objectC->property = $objB
$objectC->other = array(array('k'=>'v'));
如何获取 $objectC
中使用的类列表?
在这种特殊情况下:
array(
'Some_Class',
'Other_Class',
'Another_Class',
'stdClass',
'Another_Object'
)
我需要序列化对象,但在反序列化之前我需要实例化所有需要的类。
您如何自动获取课程?
I have an instance of the object which is dependent on other objects, e.g.
$objA = new Some_Class();
$objB = new Other_Class();
$objC = new Another_One();
$objA->property = new stdClass;
$objB->key = $objA;
$objB->arr = array(new Other_Object());
$objectC->property = $objB
$objectC->other = array(array('k'=>'v'));
How can I get a list of classes used in $objectC
?
In this particular case:
array(
'Some_Class',
'Other_Class',
'Another_Class',
'stdClass',
'Another_Object'
)
I need to serialize the object, but before unserializing I need to instantiate all the needed classes.
How would you get the classes automatically?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(1)
serialize
不是浅的,而是深的。这意味着,如果您调用
serialize($objectC);
,您不仅会获得 $objectC,还会获得它的所有属性,包括它可能包含的任何对象 。如果您必须重新实例化其中一个子对象(假设它是一个数据库适配器,其中包含不可序列化的资源),请考虑实现Serialized 接口,它允许您对序列化和反序列化操作执行细粒度的控制,这是
__sleep
/__wakeup
魔术方法。您可以使用该接口提供的方法返回自定义的数据结构,该结构将允许您根据需要手动重建对象。serialize
isn't shallow, it's deep.This means that if you call
serialize($objectC);
, you're getting not just $objectC, but also all of it's properties, including any objects that it might contain.If you must reinstantiate one of the child objects (let's say it's a database adapter, which contains an unserializable Resource), consider implementing the Serializable interface, which lets you perform fine-grained control over the serialize and unserialize operations that simply isn't possible with the
__sleep
/__wakeup
magic methods. You can use the methods provided by the interface to return a customized data structure that will allow you to manually reconstruct the object, as needed.