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)) { echo "> ERROR: $classname cannot inherit from sealed ". ( $parent->isInterface() ? 'interface' : ($parent->isTrait() ? 'trait' : 'class') ) ." ".$parent->getName()."\n"; return false; } } return true; }; if ($parent = $class->getParentClass()) { if (!$validate_against($parent)) { return; } } foreach($class->getInterfaces() as $interface) { if (!$validate_against($interface)) { return; } } foreach($class->getTraits() as $trait) { if (!$validate_against($trait)) { return; } } echo "> OK: $classname\n"; } #[Sealed(Bar::class)] interface FooInterface { } #[Sealed(Baz::class)] trait FooTrait { } final class Bar implements FooInterface {} final class Baz implements FooInterface { use FooTrait; } #[Sealed(Quux::class)] class Qux { use FooTrait; } class Quux extends Qux {} class Biz extends Qux {} validate(Bar::class); validate(Baz::class); validate(Qux::class); validate(Quux::class); validate(Biz::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
> OK: Bar > ERROR: Baz cannot inherit from sealed interface FooInterface > ERROR: Qux cannot inherit from sealed trait FooTrait > OK: Quux > ERROR: Biz cannot inherit from sealed class Qux
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: Bar > ERROR: Baz cannot inherit from sealed interface FooInterface > ERROR: Qux cannot inherit from sealed trait FooTrait > OK: Quux > ERROR: Biz cannot inherit from sealed class Qux
Output for 7.4.0 - 7.4.33
Fatal error: Uncaught Error: Call to undefined method ReflectionClass::getAttributes() in /in/Dek4H:19 Stack trace: #0 /in/Dek4H(36): {closure}(Object(ReflectionClass)) #1 /in/Dek4H(73): validate('Bar') #2 {main} thrown in /in/Dek4H 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/Dek4H on line 5
Process exited with code 255.

preferences:
160.65 ms | 401 KiB | 153 Q