PHP 8.5.8 Released!

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

表示该方法是 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()

目录

发现了问题?

了解如何改进此页面提交拉取请求报告一个错误
+添加备注

用户贡献的备注 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 によって変換されたページ (->オリジナル) /