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 __construct(object|string $objectOrMethod, string $method)
public __construct(string $classMethod)
public static createFromMethodName(string $method): static
public static export(string $class, string $name, bool $return = false): string
public getClosure(?object $object = null): Closure
public getModifiers(): int
public hasPrototype(): bool
public invoke(?object $object, mixed ...$args): mixed
public invokeArgs(?object $object, array $args): mixed
public isAbstract(): bool
public isDestructor(): bool
public isFinal(): bool
public isPrivate(): bool
public isProtected(): bool
public isPublic(): bool
public setAccessible(bool $accessible): void
public __toString(): string
/* 継承したメソッド */
}

プロパティ

name

メソッド名

class

クラス名

定義済み定数

ReflectionMethod の修飾子

ReflectionMethod::IS_STATIC int

メソッドが static であることを示します。 PHP 7.4.0 より前のバージョンでは、この値は 1 でした。

ReflectionMethod::IS_PUBLIC int

メソッドが public であることを示します。 PHP 7.4.0 より前のバージョンでは、この値は 256 でした。

ReflectionMethod::IS_PROTECTED int

メソッドが protected であることを示します。 PHP 7.4.0 より前のバージョンでは、この値は 512 でした。

ReflectionMethod::IS_PRIVATE int

メソッドが private であることを示します。 PHP 7.4.0 より前のバージョンでは、この値は 1024 でした。

ReflectionMethod::IS_ABSTRACT int

メソッドが abstract であることを示します。 PHP 7.4.0 より前のバージョンでは、この値は 2 でした。

ReflectionMethod::IS_FINAL int

メソッドが final であることを示します。 PHP 7.4.0 より前のバージョンでは、この値は 4 でした。

注意:

これらの定数の値は、PHP のバージョンが異なると変更される可能性があります。 これらの値を直接用いず、常に定数を使うことを推奨します。

変更履歴

バージョン 説明
8.4.0 クラス定数が型付けされました。
8.0.0 ReflectionMethod::export() は、削除されました。

目次

add a note

User Contributed Notes 2 notes

up
12
webseiten dot designer at googlemail dot com
14 years ago
Note that the public member $class contains the name of the class in which the method has been defined:<?phpclass A {public function __construct() {}}class B extends A {}$method = new ReflectionMethod('B', '__construct');echo $method->class; // prints 'A'?>
up
9
Anonymous
4 years ago
We can make a "Automatic dependenci injector" in classes when her constructors depends other classes (with type hint).<?phpclass 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