Example #1
0
    }
    function getStaticClosure()
    {
        return static function () {
            echo "scoped to A: ";
            var_dump(isset(A::$priv));
            echo "bound: ", isset($this) ? get_class($this) : "no";
        };
    }
}
class B extends A
{
}
$a = new A();
$staticScoped = $a->getStaticClosure();
$nonstaticScoped = $a->getClosure();
echo "Before binding", "\n";
$staticUnscoped();
echo "\n";
$nonstaticUnscoped();
echo "\n";
$staticScoped();
echo "\n";
$nonstaticScoped();
echo "\n";
echo "After binding, no instance", "\n";
$d = $staticUnscoped->bindTo(null);
$d();
echo "\n";
$d = $nonstaticUnscoped->bindTo(null);
$d();
<?php

class A
{
    function getClosure() : Closure
    {
        return function () {
        };
    }
}
$ob1 = new A();
$cl = $ob1->getClosure();
$cl2 = $cl->bindTo(null);
<?php

$closure = function ($param) {
    return "this is a closure";
};
$rf = new ReflectionFunction($closure);
var_dump($rf->getClosureScopeClass());
class A
{
    public static function getClosure()
    {
        return function ($param) {
            return "this is a closure";
        };
    }
}
$closure = A::getClosure();
$rf = new ReflectionFunction($closure);
var_dump($rf->getClosureScopeClass());
echo "Done!\n";
Example #4
0
            $this->x++;
            self::printX();
            self::print42();
            static::print42();
        };
    }
    function printX()
    {
        echo $this->x . "\n";
    }
    function print42()
    {
        echo "42\n";
    }
}
class B extends A
{
    function print42()
    {
        echo "forty two\n";
    }
}
$a = new A();
$closure = $a->getClosure();
$closure();
$b = new B();
$closure = $b->getClosure();
$closure();
?>
Done.
Example #5
0
<?php

class A
{
    private $value = 1;
    public function getClosure()
    {
        return function () {
            return $THIS->value;
        };
    }
    public function dontGetClosure()
    {
        return $THIS->value;
    }
}
$a = new A();
$fn = $a->getClosure();
echo $fn();
// 1