سؤال

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?

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top