public function __construct( public string $name, public int $population, public int $area, ) { } } class OkinawaIsland { public function __construct( public readonly string $name, public readonly int $population, public readonly int $area, ) { } } PHP8.2~ ~PHP8.1
B {} class ClassAB implements A,B {} class TryDNFArgument { public function getAandB((A&B) | null $hoge): void { echo 'Call getAandB'; } } $tryDnfArg = new TryDNFArgument(); $tryDnfArg->getAandB(new ClassAB()); //Call getAandB A&B ⬅ ClassAB OK
B { function callB(): void; } class ClassAB implements A,B { function callB(): void { var_dump('callB'); } } class TryDNFReturnValue { public function getAandB(): (A&B)| null { return new ClassAB(); } } <?php …… $tryDnfRV = new TryDNFReturnValue(); $tryDnfRV->getAandB()?->callB(); //CallB