3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); interface HasId { public string $id { get; set; } public string $displayedId { get; } } abstract class Super implements HasId { public protected(set) string $displayedId; function __construct( public string $id { set (string $value) { $this->id = $value; $this->displayedId = $value; } }, ) {} } class Foo extends Super { public protected(set) string $displayedId { get => '#' . $this->displayedId; set (string $value) => $this->displayedId = mb_strtoupper($value); } } class Bar extends Super { public string $displayedId; } class Baz { public function display(HasId $object): void { echo $object->displayedId . PHP_EOL; } public function update(HasId $object, string $id): void{ $object->id = $id; $this->display($object); } } $foo = new Foo(id: 'FOO'); $bar = new Bar(id: 'BAR'); $baz = new Baz(); $baz->display($foo); $baz->update($foo, 'foo'); $baz->display($bar); $baz->update($bar, 'bar'); $bar->displayedId = 'TEST'; $baz->display($bar); // Following statement is forbidden $foo->displayedId = 'TEST';

preferences:
151.8 ms | 1445 KiB | 7 Q