以下のコードを考えると、私はコードに伴うこのタスクに非常に固執しています:
ReptileEggが孵化すると、卵を産んだのと同じ種の新しい爬虫類が作成されます。
How can a new reptile be created inside of ->hatch when Reptile is an interface? This was on a homework assignment and I wasn't able to figure it out in time. It will drive me nuts until I go back and figure it out.
<?php
interface Reptile
{
public function layEgg() : ReptileEgg;
}
class FireDragon implements Reptile
{
public function layEgg() : ReptileEgg {
}
}
class ReptileEgg
{
public function __construct(string $reptileType)
{
}
public function hatch() : ? Reptile
{
return null;
}
}