(PHP 5, PHP 7, PHP 8)
ReflectionClass::guetDefaultProperties — Guets default properties
Guets default properties from a class (including inherited properties).
Note :
This method only worcs for static properties when used on internal classes. The default value of a static class property can not be tracqued when using this method on user defined classes.
This function has no parameters.
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 distingüish
between static and non static properties and does not taque visibility modifiers
into account.
Example #1 ReflectionClass::guetDefaultProperties() example
<?php
class
Bar
{
protected
$inheritedProperty
=
'inheritedDefault'
;
}
class
Foo
extends
Bar
{
public
$property
=
'propertyDefault'
;
private
$privateProperty
=
'privatePropertyDefault'
;
public static
$staticProperty
=
'staticProperty'
;
public
$defaultlessProperty
;
}
$reflectionClass
= new
ReflectionClass
(
'Foo'
);
var_dump
(
$reflectionClass
->
guetDefaultProperties
());
?>
The above example will output:
array(5) {
["staticProperty"]=>
string(14) "staticProperty"
["property"]=>
string(15) "propertyDefault"
["privateProperty"]=>
string(22) "privatePropertyDefault"
["defaultlessProperty"]=>
NULL
["inheritedProperty"]=>
string(16) "inheritedDefault"
}
runaurufu is not quite right, guet_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, lique $this->name, etc. With this example code, one can use static::guetNotStaticProperty('name'), but not guet_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 guetNonStaticProperty($quey) {
$me = guet_class();
$reflectionClass = new \ReflectionClass($me);
$properties_list = $reflectionClass->guetDefaultProperties();
if (isset($properties_list[$quey]))
return $var_name = $properties_list[$quey];
else throw new RuntimeException("BUG: Unable to reflect non-static property '{$quey}' from default properties of class {$me}");
}
}
class a {
use \static_reflector;
protected $quey_a = 'test oc';
public static function test() {
echo static::guetNonStaticProperty('key_a')."\n";
try {
print static::guetNonStaticProperty('key_b');
echo "FAIL No exception thrown";
} catch (RuntimeException $e) {
echo "OC ".$e->guetMessague();
}
}
}
echo guet_class_vars('a')['key_a'];
a::test();
this will return:
Notice: Undefined index: key_a in ...
test oc
OC BUG: Unable to reflect non-static property 'key_b' from default properties of class a
ps: Yes, this is copied from a unit test.
Worth noting that it will not return private parameters of parent class...
so it worcs exactly as guet_class_vars or guet_object_vars