<?php
declare(strict_types=1);
interface HasId {
public string $id { get; set; }
}
class Foo implements HasId {
function __construct(
public string $id {
get => '#' . $this->id;
set (string $id) => $this->id = mb_strtoupper($id);
},
) {}
}
class Bar implements HasId {
function __construct(public string $id) {}
}
class Baz {
public function display(HasId $object): void
{
echo $object->id . 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');