3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); spl_autoload_register(function (string $name): void { printf("autoload: %s\n", $name); switch ($name) { case 'C1': class C1 {} break; case 'C2': class C2 {} break; case 'C3': class C3 { public static function method() {} } break; case 'C4': class C4 { public const V = 1; } break; case 'C5': class C5 { public static $v; } break; case 'C6': class C6 {} break; case 'C7': class C7 {} break; case 'I1': interface I1 {} break; case 'I2': interface I2 { public const V = 1; } break; case 'I3': interface I3 {} break; case 'T1': trait T1 {} break; case 'T2': trait T2 {} break; case 'E1': enum E1: int { case V = 10; } break; case 'A1': #[Attribute] class A1 {} break; } }); final class Foo extends C1 implements I1 { use T1; } new C2; C3::method(); C4::V; C5::$v; I2::V; E1::V; // T9::V; トレイト名による定数参照でオートロードは動作するが、その後 Cannot access trait constant エラーになる new ReflectionClass(C6::class); (new ReflectionClass(Foo::class))->getAttributes('A1', ReflectionAttribute::IS_INSTANCEOF); spl_autoload_call(C7::class); unserialize('O:2:"C8":0:{}'); // クラス定義が無くてもエラーにはならない /* object(__PHP_Incomplete_Class)#2 (1) { ["__PHP_Incomplete_Class_Name"]=> string(2) "C6" } */
Output for 8.1.0 - 8.1.29, 8.2.0 - 8.2.20, 8.3.0 - 8.3.8
autoload: C1 autoload: T1 autoload: I1 autoload: C2 autoload: C3 autoload: C4 autoload: C5 autoload: I2 autoload: E1 autoload: C6 autoload: A1 autoload: C7 autoload: C8

preferences:
167.26 ms | 1411 KiB | 67 Q