@phpstan-sealedアノテーション /** @phpstan-sealed EmailContact|PhoneContact */ interface Contact {} final readonly class EmailContact implements Contact { public function __construct(public EmailAddress $email) {} } final readonly class PhoneContact implements Contact { public function __construct(public string $number) {} } // Error: Type FaxContact is not allowed to be a subtype of Contact. final readonly class FaxContact implements Contact {} 29
{ public function __construct(public string $id, public string $name) {} // 変更は、書き換えずに新しいインスタンスで返す public function withName(string $name): self { return new self($this->id, $name); } } 44