3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface A { public function foo(); } class B implements A { public function foo() { echo "foo"; } public function bar() { echo "bar"; } } class C { public function __construct(protected A $prop) { $this->prop->foo(); } } class D extends C { public function __construct(B $prop) {// Remove "protected" so that this is just an argument, not a property // Call the parent so that the field has a value parent::__construct($prop); $this->prop->bar(); } } $b = new B(); $d = new D($b);
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
foobar

preferences:
62.49 ms | 406 KiB | 5 Q