遇到未捕获的错误:调用未定义的方法Vehicles :: setPassengerSeats()

时间:2018-11-16 08:03:38

标签: php php-7

我收到此错误:

  

未定义方法的未捕获错误调用   载具:: setPassengerSeats()在   C:\ xampp \ htdocs \ practice \ vehicle.php:91。

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

这是我的源代码:

<button type="button" class="continue button validation-passed" title="Save and Continue" onclick="shipping.save();saveBilling();"><span><span>Save and Continue</span></span></button>

enter image description here

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

您不能从父级调用子级方法。您需要创建子实例以能够调用父方法

   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){
        $this->noOfDoors = $doors;
        echo "</br>The No of Doors are: {$this->noOfDoors}</br>";

        return $this->noOfDoors;
    }

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

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

$truck = new TransportationVehicles();
$truck->setNoOfVehicles("15");
$truck->setColor("Black");
$truck->setFuel("5 Litre");
$truck->setSpeed("120 km/h");
$truck->setNoOfDoors("4");
$truck->setLoadCapacity("500 KG");

$taxi = (new PassengerVehicles())->setPassengerSeats('4');

在这种情况下,您将拥有Vehicles类的两个实例+自己的孩子。

第一个实例与车辆本身+运输属性有关,例如$noOfDoors$loadCapacity-例如卡车。 第二个是基于乘用车的实例-例如出租车。

您试图从公共汽车上为乘客提供出租车的选择。