PHP 8.4.24 Released!

The ReflectionMethod class

(PHP 5, PHP 7, PHP 8)

Вступ

The ReflectionMethod class reports information about a method.

Короткий огляд класу

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 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

Method name

class

Class name

Попередньо визначені константи

ReflectionMethod Modifiers

ReflectionMethod::IS_STATIC int

Indicates that the method is static. Prior to PHP 7.4.0, the value was 1.

ReflectionMethod::IS_PUBLIC int

Indicates that the method is public. Prior to PHP 7.4.0, the value was 256.

ReflectionMethod::IS_PROTECTED int

Indicates that the method is protected. Prior to PHP 7.4.0, the value was 512.

ReflectionMethod::IS_PRIVATE int

Indicates that the method is private. Prior to PHP 7.4.0, the value was 1024.

ReflectionMethod::IS_ABSTRACT int

Indicates that the method is abstract. Prior to PHP 7.4.0, the value was 2.

ReflectionMethod::IS_FINAL int

Indicates that the method is final. Prior to PHP 7.4.0, the value was 4.

Зауваження:

The values of these constants may change between PHP versions. It is recommended to always use the constants and not rely on the values directly.

Журнал змін

Версія Опис
8.4.0 The class constants are now typed.
8.0.0 ReflectionMethod::export() was removed.

Зміст

add a note

User Contributed Notes 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) {
  }
}
To Top