(PHP 5 >= 5.4.0, PHP 7, PHP 8)
ReflectionFunctionAbstract::getClosureScopeClass — クロージャ内部のスコープに関連づけられたクラスを返す
Closure 内部のスコープに関連付けられたクラスを ReflectionClass として返します。
この関数にはパラメータはありません。
スコープが Closure 内部で使われている
クラスに関連付けられた、ReflectionClass を返します。
関数がクロージャでなかったり、グローバルスコープだった場合は
null
を代わりに返します。
例1 この例は、オブジェクトコンテキスト内部のクロージャについて、 ReflectionFunctionAbstract::getClosureCalledClass(), ReflectionFunctionAbstract::getClosureScopeClass(), ReflectionFunctionAbstract::getClosureThis() の違いを示しています。
<?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 この例は、オブジェクトコンテキストが存在しない static なクロージャについて、 ReflectionFunctionAbstract::getClosureCalledClass(), ReflectionFunctionAbstract::getClosureScopeClass(), ReflectionFunctionAbstract::getClosureThis() の違いを示しています。
<?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" }