CakeFest 2017 NYC, the Official CakePHP Conference


(PHP 5)

ReflectionClass::getDefaultPropertiesGets default properties


public array ReflectionClass::getDefaultProperties ( void )

Gets default properties from a class (including inherited properties).


This method only works for static properties when used on internal classes. The default value of a static class property can not be tracked when using this method on user defined classes.

Elenco dei parametri

Questa funzione non contiene parametri.

Valori restituiti

An array of default properties, with the key being the name of the property and the value being the default value of the property or NULL if the property doesn't have a default value. The function does not distinguish between static and non static properties and does not take visibility modifiers into account.


Example #1 ReflectionClass::getDefaultProperties() example

class Bar {
$inheritedProperty 'inheritedDefault';

Foo extends Bar {
$property 'propertyDefault';
$privateProperty 'privatePropertyDefault';
    public static 
$staticProperty 'staticProperty';

$reflectionClass = new ReflectionClass('Foo');

Il precedente esempio visualizzerĂ :

array(5) {
   string(14) "staticProperty"
   string(15) "propertyDefault"
   string(22) "privatePropertyDefault"
   string(16) "inheritedDefault"

Vedere anche:

add a note add a note

User Contributed Notes 3 notes

runaurufu AT
5 years ago
Worth noting that it will not return private parameters of parent class...
so it works exactly as get_class_vars or get_object_vars
articice at ua dot fm
11 months ago
runaurufu is not quite right, get_class_vars() does not return protected params, while this one does.

Thus it's extremely useful when having an abstract parent class and protected properties overriding in children.
For example, I use a class factory and one of the children has some static test methods that still need to output a paramether name, like $this->name, etc. With this example code, one can use static::getNotStaticProperty('name'), but not get_class_vars('name').

Try it:

trait static_reflector {
     * a purely static function that returns default properties of the non-static instance of the same class
    static protected function getNonStaticProperty($key) {
        $me = get_class();
        $reflectionClass = new \ReflectionClass($me);
        $properties_list = $reflectionClass->getDefaultProperties();
        if (isset($properties_list[$key]))
            return $var_name = $properties_list[$key];
        else throw new RuntimeException("BUG: Unable to reflect non-static property '{$key}' from default properties of class {$me}");

class a {

    use \static_reflector;

    protected $key_a = 'test ok';

    public static function test() {
        echo static::getNonStaticProperty('key_a')."\n";

        try {
            print static::getNonStaticProperty('key_b');
            echo "FAIL No exception thrown";
        } catch (RuntimeException $e) {
            echo "OK ".$e->getMessage();


echo get_class_vars('a')['key_a'];

this will return:
Notice: Undefined index: key_a in ...
test ok
OK BUG: Unable to reflect non-static property 'key_b' from default properties of class a

ps: Yes, this is copied from a unit test.
captainjester at hotmail dot com
7 years ago
This will return all properties in a class and any parent classes.  The array will have keys set to the property names and empty values.
To Top