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.ReflectionClass::getDefaultProperties
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
ReflectionClass::getDefaultProperties
Референца за `reflectionclass.getdefaultproperties.php` со подобрена типографија и навигација.
ReflectionClass::getDefaultProperties
класата mysqli_driver
ReflectionClass::getDefaultProperties — Ги добива стандардните својства
= NULL
Ги добива стандардните својства од класа (вклучувајќи ги наследените својства).
Забелешка:
Овој метод работи само за статични својства кога се користи на внатрешни класи. Стандардната вредност на статично својство на класа не може да се следи кога се користи овој метод на класи дефинирани од корисникот.
Параметри
Оваа функција нема параметри.
Вратени вредности
Еден array на стандардни својства, каде што клучот е името на својството, а вредноста е стандардната вредност на својството или null
ако својството нема стандардна вредност. Функцијата не прави разлика помеѓу статични и нестатични својства и не ги зема предвид модификаторите на видливост.
Примери
Пример #1 ReflectionClass::getDefaultProperties() 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->getDefaultProperties());
?>Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред
array(5) {
["staticProperty"]=>
string(14) "staticProperty"
["property"]=>
string(15) "propertyDefault"
["privateProperty"]=>
string(22) "privatePropertyDefault"
["defaultlessProperty"]=>
NULL
["inheritedProperty"]=>
string(16) "inheritedDefault"
}
Види Исто така
- ReflectionClass::getProperties() - Ги добива својствата
- ReflectionClass::getStaticPropertyValue() ReflectionClass::getStaticProperties()
- ReflectionClass::getProperty() - Добива ReflectionProperty за својство на класа
Белешки од корисници 2 забелешки
Worth noting that it will not return private parameters of parent class...
so it works exactly as get_class_vars or get_object_vars