3v4l.org

run code in 300+ PHP versions simultaneously
<?php class A { protected $foo = 'initial'; public function get():string {return $this->foo; } public function set(string $bar): A {$this->foo = $bar; return $this;} } class B extends A { public function set(string $bar): B {$this->foo = $bar.'!'; return $this;} } $a = new A; $b = new B; $a->set('A'); $b->set('B'); var_dump([$a->get(), $b->get()]);
Output for 7.4.12 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.28, 8.3.0 - 8.3.19, 8.4.1 - 8.4.5
array(2) { [0]=> string(1) "A" [1]=> string(2) "B!" }
Output for 7.1.10, 7.3.24 - 7.3.33
Fatal error: Declaration of B::set(string $bar): B must be compatible with A::set(string $bar): A in /in/90tor on line 11
Process exited with code 255.

preferences:
153.26 ms | 407 KiB | 5 Q