PHP 8.6.0 Beta 1 is available for testing

Класс ReflectionMethod

(PHP 5, PHP 7, PHP 8)

Введение

Класс ReflectionMethod сообщает информацию о методах.

Обзор класса

class ReflectionMethod extends ReflectionFunctionAbstract {
/* Константы */
public const int IS_STATIC ;
public const int IS_PUBLIC ;
public const int IS_PROTECTED ;
public const int IS_PRIVATE ;
public const int IS_ABSTRACT ;
public const int IS_FINAL ;
/* Свойства */
public string $class ;
/* Наследуемые свойства */
public string $name ;
/* Методы */
public function __construct (object |string $objectOrMethod, string $method)
public function __construct (string $classMethod)
public static function createFromMethodName (string $method): static
public static function export (string $class, string $name, bool $return = false ): string
public function getClosure (? object $object = null ): Closure
public function getDeclaringClass (): ReflectionClass
public function getModifiers (): int
public function getPrototype (): ReflectionMethod
public function hasPrototype (): bool
public function invoke (? object $object, mixed ...$args): mixed
public function invokeArgs (? object $object, array $args): mixed
public function isAbstract (): bool
public function isConstructor (): bool
public function isDestructor (): bool
public function isFinal (): bool
public function isPrivate (): bool
public function isProtected (): bool
public function isPublic (): bool
#[\Deprecated]
public function setAccessible (bool $accessible): void
public function __toString (): string
/* Наследуемые методы */
public function ReflectionFunctionAbstract::getAttributes (? string $name = null , int $flags = 0): array
abstract public function ReflectionFunctionAbstract::__toString (): void
}

Свойства

name

Имя метода

class

Имя класса

Предопределённые константы

Модификаторы ReflectionMethod

ReflectionMethod::IS_STATIC int

Указывает на то, что это статический метод. До PHP 7.4.0, значение было 1.

ReflectionMethod::IS_PUBLIC int

Указывает на то, что это общедоступный метод. До PHP 7.4.0, значение было 256.

ReflectionMethod::IS_PROTECTED int

Указывает на то, что это защищённый метод. До PHP 7.4.0, значение было 512.

ReflectionMethod::IS_PRIVATE int

Указывает на то, что это закрытый метод. До PHP 7.4.0, значение было 1024.

ReflectionMethod::IS_ABSTRACT int

Указывает на то, что это абстрактный метод. До PHP 7.4.0, значение было 2.

ReflectionMethod::IS_FINAL int

Указывает на то, что это окончательный метод. До PHP 7.4.0, значение было 4.

Замечание:

Значения этих констант могут изменяться от версии к версии PHP. Рекомендуется всегда использовать константы и не полагаться напрямую на значения.

Список изменений

Версия Описание
8.4.0 Константы класса теперь типизированы.
8.0.0 Метод ReflectionMethod::export() был удалён.

Содержание

Нашли ошибку?

ИнструкцияИсправлениеСообщение об ошибке
+Добавить

Примечания пользователей 2 notes

up
13
webseiten dot designer at googlemail dot com
15 years ago
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'
?>
up
9
Anonymous
5 years ago
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) {
 }
}
+Добавить

AltStyle によって変換されたページ (->オリジナル) /