Serialized PHP Reflection

2019-06-16 06:05发布

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.

2条回答
SAY GOODBYE
2楼-- · 2019-06-16 06:43

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

查看更多
Rolldiameter
3楼-- · 2019-06-16 06:52

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

查看更多
登录 后发表回答