中級 › Cannot instantiate abstract class の原因と直し方 › パターン3
Cannot instantiate abstract class の原因と直し方
abstractを付けたクラスは、そのままnewしてインスタンス化することができません。
1 <?php 2 abstract class Vehicle { 3 abstract public function wheels(): int; 4 } 5 class Car extends Vehicle { 6 public function wheels(): int { return 4; } 7 } 8 $v = new ______________(); ^ 9 echo $v->wheels();
Fatal error: Uncaught Error: Cannot instantiate abstract class Vehicle in main.php:8
空欄を埋めて実行を通す
Vehicleはabstractなのでインスタンスにできません。wheelsを実装したCarを使う必要があります。
次の問題