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.14, 8.4.16 - 8.4.17, 8.5.0 - 8.5.2
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.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.
Output for 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30
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:
84.55 ms | 411 KiB | 5 Q