3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** @template T */ class InvariantCollection { /** @var list<T> */ public $values; /** @param list<T> $values */ public function __construct($values) { $this->values = $values; } } class Foo {} class Bar extends Foo {} class FooBar { /** @var InvariantCollection<Bar> */ private $bars; public function __construct() { $this->bars = new InvariantCollection([new Bar()]); } /** @return InvariantCollection<Foo> */ public function getFoos(): InvariantCollection { $bars = $this->bars; return $bars; } public function check(): void { foreach ($this->bars->values as $bar) { if (!$bar instanceof Bar) { throw new \Exception("Illegally modified!"); } } } } $foobar = new FooBar(); $foos = $foobar->getFoos(); $foos->values[] = new Foo(); $foobar->check();
Output for git.master, git.master_jit, rfc.property-hooks
Fatal error: Uncaught Exception: Illegally modified! in /in/Z9keJ:40 Stack trace: #0 /in/Z9keJ(49): FooBar->check() #1 {main} thrown in /in/Z9keJ on line 40
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:
57.57 ms | 405 KiB | 5 Q