3v4l.org

run code in 300+ PHP versions simultaneously
<?php #[Attribute()] final class Sealed { public array $permitted = []; public function __construct(string ...$permitted) { $this->permitted = $permitted; } } function validate(string $classname): void { $class = new ReflectionClass($classname); $validate_against = static function($parent) use($classname) { if ($sealed = $parent->getAttributes(Sealed::class)) { if (!in_array($classname, $sealed[0]->newInstance()->permitted, true)) { throw new Error($classname.' cannot inherit from sealed class '.$parent->getName()); } } }; if ($parent = $class->getParentClass()) { $validate_against($parent); } foreach($class->getInterfaces() as $interface) { $validate_against($interface); } foreach($class->getTraits() as $trait) { $validate_against($trait); } echo 'ok'; } #[Sealed(Bar::class)] interface Foo { } final class Bar implements Foo {} final class Baz implements Foo {} validate(Bar::class); validate(Baz::class);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.4, 8.3.6 - 8.3.13
ok Fatal error: Uncaught Error: Baz cannot inherit from sealed class Foo in /in/uFnp8:21 Stack trace: #0 /in/uFnp8(31): {closure}(Object(ReflectionClass)) #1 /in/uFnp8(52): validate('Baz') #2 {main} thrown in /in/uFnp8 on line 21
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 ok Fatal error: Uncaught Error: Baz cannot inherit from sealed class Foo in /in/uFnp8:21 Stack trace: #0 /in/uFnp8(31): {closure}(Object(ReflectionClass)) #1 /in/uFnp8(52): validate('Baz') #2 {main} thrown in /in/uFnp8 on line 21
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Fatal error: Uncaught Error: Call to undefined method ReflectionClass::getAttributes() in /in/uFnp8:19 Stack trace: #0 /in/uFnp8(31): {closure}(Object(ReflectionClass)) #1 /in/uFnp8(51): validate('Bar') #2 {main} thrown in /in/uFnp8 on line 19
Process exited with code 255.
Output for 7.3.0 - 7.3.33
Parse error: syntax error, unexpected 'array' (T_ARRAY), expecting function (T_FUNCTION) or const (T_CONST) in /in/uFnp8 on line 5
Process exited with code 255.

preferences:
40.65 ms | 410 KiB | 5 Q