PHPのお勉強!

PHP TOP

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 resolving the class name corresponding 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 a closure in the object context

<?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, since a non-static closure take the object context
var_dump($r->getClosureScopeClass()); // Corresponds to the self::class resolution inside a closure
var_dump($r->getClosureCalledClass()); // Corresponds to the static::class resolution inside a closure

?>

上の例の出力は以下となります。

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 closure without an object context

<?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, since the pseudo-variable $this is not available in a static context
var_dump($r->getClosureScopeClass()); // Corresponds to the self::class resolution inside a closure
var_dump($r->getClosureCalledClass()); // Corresponds to the static::class resolution inside a closure

?>

上の例の出力は以下となります。

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

参考

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top