I’m trying to read protected vars by called class. Where is the problem with my protected $test and new ReflectionClass?
<?PHP
class foo
{
protected $test = ['foo' => 'foo'];
public function __construct()
{
$class = get_called_class();
do
{
foreach((new \ReflectionClass($class))->getDefaultProperties() as $property => $value)
var_dump([$class.'::'.$property => $value]);
}
while($class = get_parent_class($class));
}
}
class baz extends foo
{
protected $test = ['baz' => 'baz'];
}
new baz;
actual:
["baz::test"]=>
["baz"]=> "baz"
["foo::test"]=>
["baz"]=> "baz"
expected:
["baz::test"]=>
["baz"]=> "baz"
["foo::test"]=>
["foo"]=> "foo"
Kind regards.
There is no problem. The
$testclass variable in your parent class is set to:And the child class which inherits it overrides the value of the array (it doesn’t add to the array, but replaces all the existing keys/values) with: