ReflectionClass::getDefaultProperties

(PHP 5, PHP 7, PHP 8)

ReflectionClass::getDefaultPropertiesObtener propiedades predeterminadas

Descripción

public ReflectionClass::getDefaultProperties(): array

Devuelve las propiedades predeterminadas de una clase (incluyendo las propiedades heredadas).

Nota:

Este método sólo funciona para las propiedades estáticas cuando se utiliza en las clases internas. El valor por defecto de una propiedad de clase estática no puede ser rastreada utilizando este método sobre las clases definidas por el usuario.

Parámetros

Esta función no tiene parámetros.

Valores devueltos

Un array de propiedades predeterminadas, donde la clave es el nombre de la propiedad y el valor corresponde con el valor de la propiedad o null si la propiedad no tuviera ningún valor predeterminado. Esta función no hace distinción entre propiedades estáticas y no estáticas, y no tiene en cuenta los modificadores de visibilidad.

Ejemplos

Ejemplo #1 Ejemplo de ReflectionClass::getDefaultProperties()

<?php
class Bar {
protected
$propiedadHeredada = 'predeterminadoHeredada';
}

class
Foo extends Bar {
public
$propiedad = 'predeterminadoPropiedad';
private
$propiedadPrivada = 'predeterminadoPropiedadPrivada';
public static
$propiedadEstatica = 'propiedadEstatica';
public
$propiedadSinValor;
}

$claseReflexion = new ReflectionClass('Foo');
var_dump($claseReflexion->getDefaultProperties());
?>

El resultado del ejemplo sería:

array(5) {
   ["propiedadEstatica"]=>
   string(14) "propiedadEstatia"
   ["propiedad"]=>
   string(15) "predeterminadoPropiedad"
   ["propiedadPrivada"]=>
   string(22) "predeterminadoPropiedadPrivada"
   ["propiedadSinValor"]=>
   NULL
   ["propiedadHeredada"]=>
   string(16) "predeterminadoHeredada"
}

Ver también

add a note add a note

User Contributed Notes 3 notes

up
3
articice at ua dot fm
8 years 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'];
a::test();

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.
up
3
runaurufu AT gmail.com
12 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
up
-9
captainjester at hotmail dot com
14 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