在 PHP 中获取对象依赖关系

发布于 2024-10-22 00:44:32 字数 559 浏览 9 评论 0原文

我有一个依赖于其他对象的对象实例,例如

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ヤ经典坏疍 2024-10-29 00:44:32

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文