3v4l.org

run code in 500+ PHP versions simultaneously
<?php // Declarations and code in the global namespace namespace { class A { public function __construct($x) { echo(get_called_class().'::__construct('.$x.")\n"); } public static function someMethod($y) { echo(get_called_class().'::someMethod('.$y.")\n"); } } // No namespace, no problem $className = 'A'; $o = new $className(3); $className::someMethod('abc'); } // Use another namespace namespace N { class B extends \A {} } // Another namespace again namespace M { use N\B as B; // Use full class name $newName = 'N\B'; $o = new $newName('namespaced'); $newName::someMethod('works'); // Using a class alias works when used in code $o = new B('this works'); // This doesn't work, the class name must be full $newName = 'B'; $o = new $newName("this doesn't work"); }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
A::__construct(3) A::someMethod(abc) N\B::__construct(namespaced) N\B::someMethod(works) N\B::__construct(this works) Fatal error: Uncaught Error: Class "B" not found in /in/lg2qC:30 Stack trace: #0 {main} thrown in /in/lg2qC on line 30
Process exited with code 255.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
A::__construct(3) A::someMethod(abc) N\B::__construct(namespaced) N\B::someMethod(works) N\B::__construct(this works) Fatal error: Uncaught Error: Class 'B' not found in /in/lg2qC:30 Stack trace: #0 {main} thrown in /in/lg2qC on line 30
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
A::__construct(3) A::someMethod(abc) N\B::__construct(namespaced) N\B::someMethod(works) N\B::__construct(this works) Fatal error: Class 'B' not found in /in/lg2qC on line 30
Process exited with code 255.

preferences:
116.09 ms | 2238 KiB | 4 Q