PHP 从扩展中添加属性 Class / Parents' Class

PHP Add properties from Extended Class / Parents' Class

我有一个项目,我尝试将 parents class(vehicle.php) 中的属性实现到 landvehicle.php 中。所有属性都设置为 public。在这种特殊情况下,我的目标是将 Vehicle 的属性用于 Landvehicle,用于 $landOne ($height)。

Vehicle.php :

<?php

class Vehicle
{
    public $length;
    public $height;
    public $color;
    public $weight;
    public $price;
    public $range;


    public function __construct($length, $height, $color, $weight, $price, $range)
    {
        $this->length = $length;
        $this->height = $height;
        $this->color = $color;
        $this->weight = $weight;
        $this->price = $price;
        $this->range = $range;
    }
    public function addHeight()
    {
        return "$this->height is the height";
    }


}

$userVehicleOne = new Vehicle('2.5 Meter', '1,3 Meter', 'red', '1800kg', '40.000€', '450km');

土地vehicle.php:

<?php

include 'vehicle.php';

class Landvehicle extends Vehicle
{
    public $amountWheels;
    public $movementType;

    public function __construct($amountWheels, $movementType)
    {
        $this->amountWheels = $amountWheels;
        $this->movementType = $movementType;
    }

}

$landOne = new Landvehicle ('4', 'flexible');



echo $landOne->addHeight('1.50Meter')

我尝试使用 Vehicle.php 的 addHeight 函数将 $landOne

的高度设置为 1.50 米

在 addHeight 方法中您不接受任何参数,在 Landvehicle.php 中您将 1.50Meter 作为第一个参数传递,这是不正确的。删除您的 addHeight 方法并将其替换为以下代码:

public function addHeight($height)
{
    $this->height = $height;
    return "$this->height is the height";
}