array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'uk', ), 'this' => array ( 0 => 'reflectionfunctionabstract.getclosurethis.php', 1 => 'ReflectionFunctionAbstract::getClosureThis', 2 => 'Returns the object which corresponds to $this inside a closure', ), 'up' => array ( 0 => 'class.reflectionfunctionabstract.php', 1 => 'ReflectionFunctionAbstract', ), 'prev' => array ( 0 => 'reflectionfunctionabstract.getclosurescopeclass.php', 1 => 'ReflectionFunctionAbstract::getClosureScopeClass', ), 'next' => array ( 0 => 'reflectionfunctionabstract.getclosureusedvariables.php', 1 => 'ReflectionFunctionAbstract::getClosureUsedVariables', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'en', 'path' => 'reference/reflection/reflectionfunctionabstract/getclosurethis.xml', ), 'history' => array ( ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); contributors($setup); ?>
(PHP 5 >= 5.4.0, PHP 7, PHP 8)
ReflectionFunctionAbstract::getClosureThis — Returns the object which corresponds to $this inside a closure
If the function is a non-static closure, get the object bound to $this inside the closure.
У цієї функції немає параметрів.
Return the object instance represented by $this inside
the Closure.
If the function is not a closure or if it has no $this null
is returned instead.
Приклад #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
?>Поданий вище приклад виведе:
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 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
?>Поданий вище приклад виведе:
string(1) "A"
string(1) "B"
NULL
object(ReflectionClass)#4 (1) {
["name"]=>
string(1) "A"
}
object(ReflectionClass)#4 (1) {
["name"]=>
string(1) "B"
}