PHP 8.4.0 RC4 available for testing

ReflectionFunctionAbstract::getClosureScopeClass

(PHP 5 >= 5.4.0, PHP 7, PHP 8)

ReflectionFunctionAbstract::getClosureScopeClassReturns the class corresponding to the scope inside a closure

Beschreibung

public ReflectionFunctionAbstract::getClosureScopeClass(): ?ReflectionClass

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

Parameter-Liste

Diese Funktion besitzt keine Parameter.

Rückgabewerte

Returns a ReflectionClass corresponding to the class whose scope is being used inside the Closure. If the function is not a closure or if it has global scope null is returned instead.

Beispiele

Beispiel #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

?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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"
}

Beispiel #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

?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

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

Siehe auch

add a note

User Contributed Notes

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