首页 > 解决方案 > 获取未捕获的错误:调用未定义的方法 Vehicles::setPassengerSeats()

问题描述

我收到此错误:

在 C:\xampp\htdocs\practice\vehicle.php:91 中对取消定义方法 Vehicles::setPassengerSeats() 的未捕获错误调用。

我还添加了错误的屏幕截图。请检查并告诉我如何解决?我想我的子类有问题,但我不知道在哪里?

这是我的源代码:

<?php  

class Vehicles{
    private $noOfVehicles;
    private $color;
    private $fuel;
    private $speed;

    public function getNoOfVehicles(){
        return $this->noOfMobiles;
    }

    public function setNoOfVehicles($Vehicles){
        $this->noOfMobiles = $Vehicles;
        echo "No of Vehicles are: ".$this->noOfVehicles."</br>";
    }

    public function getColor(){
        return $this->color;
    }

    public function setColor($look){
        $this->color = $look;
        echo "</br>The Color of Vehicle is: ".$this->color."</br>";
    }

    public function getFuel(){
        return $this->fuel;
    }

    public function setFuel($petrol){
        $this->fuel = $petrol;
        echo "</br>The fuel is: ".$this->color."</br>";
    }

    public function getSpeed(){
        return $this->speed;
    }

    public function setSpeed($vehicleSpeed){
        $this->speed = $vehicleSpeed;
        echo "</br>The speed of vehicle is: ".$this->speed."</br>";
    }

}

class PassengerVehicles extends Vehicles{
    private $passengerSeats;

    public function getPassengerSeats(){
        return $this->passengerSeats;
    }

    public function setPassengerSeats($seats){
        return $this->passengerSeats = $seats;
        echo "</br>Passenger Seats are: ".$this->passengerSeats."</br>";
    }

}

class TransportationVehicles extends Vehicles{
    private $noOfDoors;
    private $loadCapacity;

    public function getNoOfDoors(){
        return $this->noOfDoors;
    }

    public function setNoOfDoors($doors){
        return $this->noOfDoors = $doors;
        echo "</br>The No of Doors are: ".$this->noOfDoors."</br>";
    }

    public function getLoadCapacity(){
        return $this->loadCapacity;
    }

    public function setLoadCapacity($capacity){
        return $this->loadCapacity = $capacity;
        echo "The Load Capacity is: ".$this->loadCapacity."</br>";
    }
}

$VehiclesObj = new Vehicles;
$VehiclesObj->setNoOfVehicles("15");
$VehiclesObj->setColor("Black");
$VehiclesObj->setFuel("5 Litre");
$VehiclesObj->setSpeed("120 km/h");

$VehiclesObj->setPassengerSeats("4");
$VehiclesObj->setNoOfDoors("4");
$VehiclesObj->setLoadCapacity("500 KG");

?>

在此处输入图像描述

标签: phpphp-7

解决方案


您调用setPassengerSeats不在另一个类中的方法Vehicles您应该首先创建实例,然后调用此方法:

$passangerVehicle = new PassengerVehicles;
$passangerVehicle->setPassengerSeats("4");

推荐阅读