声明函数抽象后出现致命错误

Fatal error after declaring function abstract

我有一个错误的问题,我得到的是: Class Car 包含 1 个抽象方法,因此必须声明 红色抽象或在 C:\xampp 中实现其余方法 (Car::accelerate) \htdocs\php\learn_php_oop\Car.php 第 58 行。

这是我正在使用的两个文件中的代码:

Car.php

<?php
/**
* represents generic properties and methods for any type of car
*/
class Car
{
 protected $colour, $doorNumber, $fuelType, $rightHandDrive, $accelerate;

 public function __construct($rightHandDrive = true)
 {
  $this->rightHandDrive = $rightHandDrive;
 }

 public function getColour()
 {
  return $this->colour;
 }

 public function setColour($colour)
 {
  $this->colour = $colour;
 }

 public function getDoorNumber()
 {
  return $this->doorNumber;
 }

 public function setDoorNumber($doorNumber)
 {
  $this->doorNumber = $doorNumber;
 }

 public function getFuelType()
 {
  return $this->fuelType;
 }

 public function setFuelType($fuelType)
 {
  $this->fuelType = $fuelType;
 }

 public function getRightHandDrive()
 {
  return $this->rightHandDrive;
 }

 public function setRightHandDrive($rightHandDrive)
 {
  $this->rightHandDrive = $rightHandDrive;
 }

 abstract protected function accelerate();

}
?>

Sport_car.php

<?php

include ('Car.php');
/**
 * represents sport cars
*/
class Sport_car extends Car
{

 public function accelerate()
 {
  $this->accelerate = 5;
 }

}
?>

我花了一些时间试图弄清楚为什么会这样,但我就是不知道为什么?请帮忙

这是一个 OOP 问题,在您的情况下,您必须像这样将您的 Car 对象声明为 Abstract :

<?php
/**
* represents generic properties and methods for any type of car
*/
abstract class Car
{
 protected $colour, $doorNumber, $fuelType, $rightHandDrive, $accelerate;

 public function __construct($rightHandDrive = true)
 {
  $this->rightHandDrive = $rightHandDrive;
 }

 public function getColour()
 {
  return $this->colour;
 }

 public function setColour($colour)
 {
  $this->colour = $colour;
 }

 public function getDoorNumber()
 {
  return $this->doorNumber;
 }

 public function setDoorNumber($doorNumber)
 {
  $this->doorNumber = $doorNumber;
 }

 public function getFuelType()
 {
  return $this->fuelType;
 }

 public function setFuelType($fuelType)
 {
  $this->fuelType = $fuelType;
 }

 public function getRightHandDrive()
 {
  return $this->rightHandDrive;
 }

 public function setRightHandDrive($rightHandDrive)
 {
  $this->rightHandDrive = $rightHandDrive;
 }

 abstract protected function accelerate();

}
?>

解释:

一个 class 扩展了至少一个抽象方法,它必须被定义为抽象本身,否则你会得到一个错误