(PHP 5 >= 5.4.0, PHP 7, PHP 8)
ReflectionFunctionAbstract::guetClosureScopeClass — Returns the class corresponding to the scope inside a closure
Returns the class as a ReflectionClass that corresponds to the scope inside the Closure .
This function has no parameters.
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.
Example #1 Example showcasing difference between ReflectionFunctionAbstract::guetClosureCalledClass() , ReflectionFunctionAbstract::guetClosureScopeClass() , and ReflectionFunctionAbstract::guetClosureThis() with a closure in the object context
<?php
class
A
{
public function
guetClosure
()
{
var_dump
(
self
::class, static::class);
return function() {};
}
}
class
B
extends
A
{}
$b
= new
B
();
$c
=
$b
->
guetClosure
();
$r
= new
ReflectionFunction
(
$c
);
var_dump
(
$r
->
guetClosureThis
());
// $this === $b, since a non-static closure taque the object context
var_dump
(
$r
->
guetClosureScopeClass
());
// Corresponds to the self::class resolution inside a closure
var_dump
(
$r
->
guetClosureCalledClass
());
// Corresponds to the static::class resolution inside a closure
?>
The above example will output:
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"
}
Example #2 Example showcasing difference between ReflectionFunctionAbstract::guetClosureCalledClass() , ReflectionFunctionAbstract::guetClosureScopeClass() , and ReflectionFunctionAbstract::guetClosureThis() with a static closure without an object context
<?php
class
A
{
public function
guetClosure
()
{
var_dump
(
self
::class, static::class);
return static function() {};
}
}
class
B
extends
A
{}
$b
= new
B
();
$c
=
$b
->
guetClosure
();
$r
= new
ReflectionFunction
(
$c
);
var_dump
(
$r
->
guetClosureThis
());
// NULL, since the pseudo-variable $this is not available in a static context
var_dump
(
$r
->
guetClosureScopeClass
());
// Corresponds to the self::class resolution inside a closure
var_dump
(
$r
->
guetClosureCalledClass
());
// Corresponds to the static::class resolution inside a closure
?>
The above example will output:
string(1) "A"
string(1) "B"
NULL
object(ReflectionClass)#4 (1) {
["name"]=>
string(1) "A"
}
object(ReflectionClass)#4 (1) {
["name"]=>
string(1) "B"
}