ReflectionFunctionAbstract::getClosureCalledClass

クロージャ内部で static:: に対応するクラスを返す

説明

public ReflectionClassnull ReflectionFunctionAbstract::getClosureCalledClass()

Closure 内部で static:: に対応するクラス名に解決されるクラスを、ReflectionClass として返します。

パラメータ

この関数にはパラメータはありません。

戻り値

Closure 内部で static:: で表現されるクラスに対応する 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"
}

参考