ReflectionFunctionAbstract::getClosureCalledClass

(PHP 8 >= 8.0.23, PHP 8 >= 8.1.11)

ReflectionFunctionAbstract::getClosureCalledClassReturns the class corresponding to static:: inside a closure

说明

public ReflectionFunctionAbstract::getClosureCalledClass(): ?ReflectionClass

Returns the class as a ReflectionClass that corresponds to static:: inside the Closure.

参数

此函数没有参数。

返回值

Returns a ReflectionClass corresponding to the class represented by static:: in the Closure. If the function is not a closure or if it has global scope null is returned instead.

示例

示例 #1 Example showcasing difference between ReflectionFunctionAbstract::getClosureCalledClass(), ReflectionFunctionAbstract::getClosureScopeClass(), and ReflectionFunctionAbstract::getClosureThis() with an instance method

<?php

class A {
public function
getClosure() {
var_dump(self::class, static::class);
return function () {

};
}
}

class
B extends A {

}

$b = new B();
$c = $b->getClosure();
$r = new ReflectionFunction($c);
var_dump($r->getClosureThis()); // $this === $b
var_dump($r->getClosureScopeClass()); // self::class
var_dump($r->getClosureCalledClass()); // static::class

?>

以上示例会输出:

string(1) "A"
string(1) "B"
object(B)#1 (0) {
}
object(ReflectionClass)#4 (1) {
  ["name"]=>
  string(1) "A"
}
object(ReflectionClass)#4 (1) {
  ["name"]=>
  string(1) "B"
}

示例 #2 Example showcasing difference between ReflectionFunctionAbstract::getClosureCalledClass(), ReflectionFunctionAbstract::getClosureScopeClass(), and ReflectionFunctionAbstract::getClosureThis() with a static method

<?php

class A {
public function
getClosure() {
var_dump(self::class, static::class);
return static function () {

};
}
}

class
B extends A {

}

$b = new B();
$c = $b->getClosure();
$r = new ReflectionFunction($c);
var_dump($r->getClosureThis()); // NULL
var_dump($r->getClosureScopeClass()); // self::class
var_dump($r->getClosureCalledClass()); // static::class

?>

以上示例会输出:

string(1) "A"
string(1) "B"
NULL
object(ReflectionClass)#4 (1) {
  ["name"]=>
  string(1) "A"
}
object(ReflectionClass)#4 (1) {
  ["name"]=>
  string(1) "B"
}

参见

添加备注

用户贡献的备注

此页面尚无用户贡献的备注。
To Top