3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Demo { } // An "autoload" handler creating an alias for the same class spl_autoload_register(function ($class) { echo "autoload $class\n"; class_alias('Demo', $class); }); // Create a Demo object using "A" // and pass that to function expecting "A" function success(A $test) { echo "Success ".get_class($test)."\n"; } echo "1\n"; success(new A()); // Create a Demo object using "B", // then create another Demo object using "C" // and pass that to function expecting "B" function also(B $test) { echo "Success ".get_class($test)."\n"; } echo "2\n"; new B(); echo "3\n"; also(new C()); // Create a Demo object using "X", // and pass that to function expecting "B" function fail(X $test) { echo "Success ".get_class($test)."\n"; } echo "4\n"; fail(new Y()); // Note that all of these names refer to the same class, // and all objects are of the same class
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
1 autoload A Success Demo 2 autoload B 3 autoload C Success Demo 4 autoload Y Fatal error: Uncaught TypeError: fail(): Argument #1 ($test) must be of type X, Demo given, called in /in/NuX6K on line 36 and defined in /in/NuX6K:32 Stack trace: #0 /in/NuX6K(36): fail(Object(Demo)) #1 {main} thrown in /in/NuX6K on line 32
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 1 autoload A Success Demo 2 autoload B 3 autoload C Success Demo 4 autoload Y Fatal error: Uncaught TypeError: fail(): Argument #1 ($test) must be of type X, Demo given, called in /in/NuX6K on line 36 and defined in /in/NuX6K:32 Stack trace: #0 /in/NuX6K(36): fail(Object(Demo)) #1 {main} thrown in /in/NuX6K on line 32
Process exited with code 255.
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
1 autoload A Success Demo 2 autoload B 3 autoload C Success Demo 4 autoload Y Fatal error: Uncaught TypeError: Argument 1 passed to fail() must be an instance of X, instance of Demo given, called in /in/NuX6K on line 36 and defined in /in/NuX6K:32 Stack trace: #0 /in/NuX6K(36): fail(Object(Demo)) #1 {main} thrown in /in/NuX6K on line 32
Process exited with code 255.

preferences:
143.69 ms | 402 KiB | 156 Q