3v4l.org

run code in 300+ PHP versions simultaneously
<?php class foo { protected $bar = 'A bar'; public function getBarDumper() { $that = $this; return function() use ($that) { var_dump($that->bar); }; } public static function getFactory() { // la fonction anonyme suivante est statique // car elle est déclarée dans une méthode statique. return function() { return new static(); }; } } $factory = foo::getFactory(); $foo = $factory(); $bar = $foo->getBarDumper(); $bar(); ?>

preferences:
50.45 ms | 402 KiB | 5 Q