The ReflectionMethod class
(PHP 5)
Introduction
The ReflectionMethod class reports information about a method.
Class synopsis
Properties
- name
-
Method name
- class
-
Class name
Predefined Constants
ReflectionMethod Modifiers
ReflectionMethod::IS_STATIC
-
Indicates that the method is static.
ReflectionMethod::IS_PUBLIC
-
Indicates that the method is public.
ReflectionMethod::IS_PROTECTED
-
Indicates that the method is protected.
ReflectionMethod::IS_PRIVATE
-
Indicates that the method is private.
ReflectionMethod::IS_ABSTRACT
-
Indicates that the method is abstract.
ReflectionMethod::IS_FINAL
-
Indicates that the method is final.
Table of Contents
- ReflectionMethod::__construct — Constructs a ReflectionMethod
- ReflectionMethod::export — Export a reflection method.
- ReflectionMethod::getClosure — Returns a dynamically created closure for the method
- ReflectionMethod::getDeclaringClass — Gets declaring class for the reflected method.
- ReflectionMethod::getModifiers — Gets the method modifiers
- ReflectionMethod::getPrototype — Gets the method prototype (if there is one).
- ReflectionMethod::invoke — Invoke
- ReflectionMethod::invokeArgs — Invoke args
- ReflectionMethod::isAbstract — Checks if method is abstract
- ReflectionMethod::isConstructor — Checks if method is a constructor
- ReflectionMethod::isDestructor — Checks if method is a destructor
- ReflectionMethod::isFinal — Checks if method is final
- ReflectionMethod::isPrivate — Checks if method is private
- ReflectionMethod::isProtected — Checks if method is protected
- ReflectionMethod::isPublic — Checks if method is public
- ReflectionMethod::isStatic — Checks if method is static
- ReflectionMethod::setAccessible — Set method accessibility
- ReflectionMethod::__toString — Returns the string representation of the Reflection method object.
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения, относящиеся к переменным и типам
- Введение
- Установка и настройка
- Предопределенные константы
- Примеры
- Расширение
- Класс Reflection
- Класс ReflectionClass
- Класс ReflectionZendExtension
- Класс ReflectionExtension
- Класс ReflectionFunction
- Класс ReflectionFunctionAbstract
- Класс ReflectionMethod
- Класс ReflectionObject
- Класс ReflectionParameter
- Класс ReflectionProperty
- ReflectionType
- ReflectionGenerator
- Интерфейс Reflector
- Класс ReflectionException
Коментарии
Note that the public member $class contains the name of the class in which the method has been defined:
<?php
class A {public function __construct() {}}
class B extends A {}
$method = new ReflectionMethod('B', '__construct');
echo $method->class; // prints 'A'
?>
We can make a "Automatic dependenci injector" in classes when her constructors depends other classes (with type hint).
<?php
class Dependence1 {
function foo() {
echo "foo";
}
}
class Dependence2 {
function foo2() {
echo "foo2";
}
}
final class myClass
{
private $dep1;
private $dep2;
public function __construct(
Dependence1 $dependence1,
Dependence2 $dependence2
)
{
$this->dep1 = $dependence1;
$this->dep2 = $dependence2;
}
}
// Automatic dependence injection (CLASSES)
$constructor = new ReflectionMethod(myClass::class, '__construct');
$parameters = $constructor->getParameters();
$dependences = [];
foreach ($parameters as $parameter) {
$dependenceClass = (string) $parameter->getType();
$dependences[] = new $dependenceClass();
}
$instance = new myClass(...$dependences);
var_dump($instance);
?>
Results in:
object(myClass)#6 (2) {
["dep1":"myClass":private]=>
object(Dependence1)#4 (0) {
}
["dep2":"myClass":private]=>
object(Dependence2)#5 (0) {
}
}