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 git.master, git.master_jit, rfc.property-hooks
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.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
67.71 ms | 405 KiB | 5 Q