mercredi 31 mai 2017

PHP: get property default value constant name using Reflection API

I need to be able to retrieve property default value constant name. For example, having such class:

class Foo
{
    const BAR = 'BAR';
    private $baz = self::BAR;
}

I want to be able to do something like follows:

$reflection = new \ReflectionClass(Foo::class);
$reflection->getProperty('baz')->getDefaultValueConstantName(); // 'Foo::BAR'

The weird thing about PHP Reflection API is that \ReflectionParameter class has method getDefaultValueConstantName(), but \ReflectionProperty does not. I can get property default value using \ReflectionClass::getDefaultProperties(), but this method will return property default value, not constant name this value has been taken from. Is there any solution for getting property default value constant name, and is this feature at least planned for next releases of PHP?





Aucun commentaire:

Enregistrer un commentaire