Serialized PHP Reflection

2019-06-16 06:16发布

问题:

Please consider the following example code:

<?php

class a {
    function b() {}
}

$r=new ReflectionMethod(new a, "b");
var_dump($r->getParameters());
$s=serialize($r);
$r=unserialize($s);
var_dump($r->getParameters());

?>

That produces the following output:

array(0) { }
Fatal error: ReflectionFunctionAbstract::getParameters() [<a href='reflectionfunctionabstract.getparameters'>reflectionfunctionabstract.getparameters</a>]: Internal error: Failed to retrieve the reflection object in [...]test.php on line 13

The question stands, is there a way to correctly serialize/unserialize Reflection objects in PHP?

Thank you.

回答1:

from http://bugs.php.net/bug.php?id=30324

Serialization doesn't work on virtual properties and this problem usually occurs with internal classes.

Maybe that is the case with this method. However, I'm not sure the documentation is not clear. http://in3.php.net/manual/en/reflectionfunctionabstract.getparameters.php



回答2:

Use the JMS Serializer to serialize private and protected properties and also virtual properties using methods.