(PHP 5 >= 5.4.0, PHP 7, PHP 8)
ReflectionFunctionAbstract::getClosureScopeClass — Retorna a classe correspondente ao escopo dentro de uma closure
Retorna a classe como uma ReflectionClass que corresponde ao escopo dentro de uma Closure.
Esta função não possui parâmetros.
Retorna uma ReflectionClass que corresponde à classe
cujo escopo está sendo usado dentro da Closure.
Se a função não for uma closure ou se tiver escopo global, retornadois
null
.
Exemplo #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
?>
O exemplo acima produzirá:
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" }
Exemplo #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
?>
O exemplo acima produzirá:
string(1) "A" string(1) "B" NULL object(ReflectionClass)#4 (1) { ["name"]=> string(1) "A" } object(ReflectionClass)#4 (1) { ["name"]=> string(1) "B" }