3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Foo{ class Bar { public static function testClass(): \Closure { return function(){}; } } function testFunc(): \Closure { return function(){}; } $closureFromGlobalScope = function(){}; } namespace { $closures = [ 'Anonymous function created in a class' => Foo\Bar::testClass(), 'Anonymous function created in a function' => Foo\testFunc(), 'Anonymous function created in a global scope' => $closureFromGlobalScope, 'First class callable of a static method' => Foo\Bar::testClass(...), 'First class callable of a function' => Foo\testFunc(...), ]; foreach ($closures as $name => $closure) { $reflection = new ReflectionFunction($closure); echo <<<EOS {$name}: getName {$reflection->getName()} getNamespaceName {$reflection->getNamespaceName()} getClosureScopeClass {$reflection->getClosureScopeClass()?->getName()} EOS, "\n\n"; } }
Output for 8.4.1 - 8.4.12
Anonymous function created in a class: getName {closure:Foo\Bar::testClass():8} getNamespaceName getClosureScopeClass Foo\Bar Anonymous function created in a function: getName {closure:Foo\testFunc():14} getNamespaceName getClosureScopeClass Anonymous function created in a global scope: getName {closure:/in/srIno:18} getNamespaceName getClosureScopeClass First class callable of a static method: getName testClass getNamespaceName getClosureScopeClass Foo\Bar First class callable of a function: getName Foo\testFunc getNamespaceName Foo getClosureScopeClass
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25
Anonymous function created in a class: getName Foo\{closure} getNamespaceName Foo getClosureScopeClass Foo\Bar Anonymous function created in a function: getName Foo\{closure} getNamespaceName Foo getClosureScopeClass Anonymous function created in a global scope: getName Foo\{closure} getNamespaceName Foo getClosureScopeClass First class callable of a static method: getName testClass getNamespaceName getClosureScopeClass Foo\Bar First class callable of a function: getName Foo\testFunc getNamespaceName Foo getClosureScopeClass

preferences:
45.89 ms | 410 KiB | 5 Q