/** * Returns an associative array containing this method's static variables * and their values * * @return array<sting,mixed> This method's static variables */ public function getStaticVariables() { if ($this->reflectionSource instanceof ReflectionMethod) { return $this->reflectionSource->getStaticVariables(); } else { return parent::getStaticVariables(); } }
}; echo "---- ReflectionFunction ----\n"; $rf = new ReflectionFunction('foo'); echo "ReflectionFunction(1):"; var_dump($rf->getStaticVariables()); foo(); echo "ReflectionFunction(2):"; var_dump($rf->getStaticVariables()); foo(); $rf = new ReflectionFunction('foo'); echo "ReflectionFunction(3):"; var_dump($rf->getStaticVariables()); echo "---- ReflectionMethod ----\n"; $rf = new ReflectionMethod('Bar', 'foo'); echo "ReflectionMethod(1):"; var_dump($rf->getStaticVariables()); Bar::foo(); echo "ReflectionMethod(2):"; var_dump($rf->getStaticVariables()); Bar::foo(); $rf = new ReflectionFunction('foo'); echo "ReflectionMethod(3):"; var_dump($rf->getStaticVariables()); echo "---- ReflectionClosure ----\n"; $rf = new ReflectionFunction($foo); echo "ReflectionFunction(1-closure):"; var_dump($rf->getStaticVariables()); $foo(); echo "ReflectionFunction(2-closure):"; var_dump($rf->getStaticVariables()); $foo();
<pre> <?php class Counter { private static $c = 0; public static final function increment() { return ++self::$c; } } // Создание экземпляра класса ReflectionMethod $method = new ReflectionMethod('Counter', 'increment'); // exit; // Вывод основной информации printf("===> %s%s%s%s%s%s%s метод '%s' (который является %s)\n" . " объявлен в %s\n" . " строки с %d по %d\n" . " имеет модификаторы %d[%s]\n", $method->isInternal() ? 'Встроенный' : 'Пользовательский', $method->isAbstract() ? ' абстрактный' : '', $method->isFinal() ? ' финальный' : '', $method->isPublic() ? ' public' : '', $method->isPrivate() ? ' private' : '', $method->isProtected() ? ' protected' : '', $method->isStatic() ? ' статический' : '', $method->getName(), $method->isConstructor() ? 'конструктором' : 'обычным методом', $method->getFileName(), $method->getStartLine(), $method->getEndline(), $method->getModifiers(), implode(' ', Reflection::getModifierNames($method->getModifiers()))); // Вывод статических переменных, если они есть if ($statics = $method->getStaticVariables()) { printf("---> Статическая переменная: %s\n", var_export($statics, 1)); } // Вызов метода printf("---> Результат вызова: "); $result = $method->invoke(3); echo $result; ?> </pre>
} catch (TypeError $re) { echo "Ok - " . $re->getMessage() . PHP_EOL; } try { new ReflectionMethod('a', 'b', 'c'); } catch (TypeError $re) { echo "Ok - " . $re->getMessage() . PHP_EOL; } class C { public function f() { } } $rm = new ReflectionMethod('C', 'f'); var_dump($rm->isFinal(1)); var_dump($rm->isAbstract(1)); var_dump($rm->isPrivate(1)); var_dump($rm->isProtected(1)); var_dump($rm->isPublic(1)); var_dump($rm->isStatic(1)); var_dump($rm->isConstructor(1)); var_dump($rm->isDestructor(1)); var_dump($rm->getModifiers(1)); var_dump($rm->isInternal(1)); var_dump($rm->isUserDefined(1)); var_dump($rm->getFileName(1)); var_dump($rm->getStartLine(1)); var_dump($rm->getEndLine(1)); var_dump($rm->getStaticVariables(1)); var_dump($rm->getName(1));
<?php $a = 4; $b = 'i am a string'; function fn($c) { return $c + 5; } $lam = function ($c) use($a, $b) { return $a + $c; }; class C { public static $i; public $j; public function meth($c) { return $c + 3; } } $refl = new ReflectionFunction('fn'); var_dump($refl->getStaticVariables()); $refl = new ReflectionFunction($lam); var_dump($refl->getStaticVariables()); $refl = new ReflectionMethod('C', 'meth'); var_dump($refl->getStaticVariables());
class TestClass { public function foo() { static $c; static $a = 1; static $b = "hello"; $d = 5; } private function bar() { static $a = 1; } public function noStatics() { $a = 54; } } echo "Public method:\n"; $methodInfo = new ReflectionMethod('TestClass::foo'); var_dump($methodInfo->getStaticVariables()); echo "\nPrivate method:\n"; $methodInfo = new ReflectionMethod('TestClass::bar'); var_dump($methodInfo->getStaticVariables()); echo "\nMethod with no static variables:\n"; $methodInfo = new ReflectionMethod('TestClass::noStatics'); var_dump($methodInfo->getStaticVariables()); echo "\nInternal Method:\n"; $methodInfo = new ReflectionMethod('ReflectionClass::getName'); var_dump($methodInfo->getStaticVariables());
<?php function dummy() { static $a = array(); } class Test { const A = 0; public function func() { static $a = array(self::A => 'a'); } } $reflect = new ReflectionFunction("dummy"); print_r($reflect->getStaticVariables()); $reflect = new ReflectionMethod('Test', 'func'); print_r($reflect->getStaticVariables());