I failed to get dynamic instance variables via PHP's reflection
Sample code:
<?php
class Foo
{
public function bar()
{
$reflect = new ReflectionClass($this);
$props = $reflect->getProperties();
var_export($props);
die;
}
}
$foo = new Foo();
$foo->a = "a";
$foo->b = "b";
$foo->bar(); // Failed to print out variable a and b
Any idea?
ReflectionClass::getProperties()
gets only properties explicitly defined by a class. To reflect all properties you set on an object, use ReflectionObject
which inherits from ReflectionClass
and works on runtime instances:
$reflect = new ReflectionObject($this);
Or as Tim Cooper says, forget reflection and just use get_object_vars()
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With