如何为对象创建默认值? PHP

how to create default value for for an object? PHP

假设我有一个人 class,它有 $gender 变量,但没有分配任何值。 Human 有一个参数为年龄、性别、身高和体重的构造函数。

我有另一个名为 Female 的 class,它继承自 Human,但现在 Female class 正在用 Female.

字符串覆盖 $gender 变量

当我创建对象时,假设 $f = new Female(12, 'female', 123, 40); 如何在创建对象时跳过输入女性?

我认为我们需要在 Female class 中创建另一个新的构造函数,我在 Female class 构造函数的参数中创建了 age, gender = 'female', height and weight 但这似乎不起作用.

我尝试在创建对象时将性别部分留空或尝试输入空字符串,例如 ""

有人可以帮帮我吗?非常感谢。

我人类的代码class

class Human {
    protected $age = 0;   
    protected $gender;
    protected $height_in_cm;
    protected $weight_in_kg;

    function __construct($age, $gender, $heightCM, $weightKG)
    {
        $this->age = $age;
        $this->gender = $gender;
        $this->height_in_cm = $heightCM;
        $this->weight_in_kg = $weightKG;
    }

    /**
     * @return int
     */
    public function getAge()
    {
        return $this->age;
    }

    /**
     * @return string
     */
    public function getGender()
    {
        return $this->gender;
    }
}

女性代码class

require_once('Human.php');
class Female extends Human{
    protected $gender = 'female';

    function __construct($age, $gender = 'female', $heightCM, $weightKG)
    {
        $this->age = $age;
        $this->gender = $gender;
        $this->height_in_cm = $heightCM;
        $this->weight_in_kg = $weightKG;
    }
}

$f = new Female(12,'female',123,40);
echo "Your gender is ". $f->getGender()."<br>";

我想,在你的构造函数中

__construct($var, $gender="female", $var, $var){
//rest assignment
}

应该做

或者,使用已设置 female 的 3 参数构造函数

您可以简单地覆盖构造函数:

abstract class Human
{
    protected $age;

    protected $gender;

    protected $height;

    protected $weight;

    public function __construct($age, $gender, $height, $weight)
    {
        $this->age = $age;
        $this->gender = $gender;
        $this->height = $height;
        $this->weight = $weight;
    }

    public function getGender()
    {
        return $this->gender;
    }
}

class Female extends Human
{
    public function __construct($age, $height, $weight)
    {
        parent::__construct($age, 'female', $height, $weight);
    }
}

$female = new Female(12, 170, 60);
echo $female->getGender();

您可以在扩展中设置值 class:

// If it should be possible to instantiate the Human
// class then remove the "abstract" thing at set the
// "gender" property in the constructer
abstract class Human {
    protected $gender;
    protected $age;
    public function __construct($age) {
        $this->age = $age;
    }
}
class Female extends Human {
    protected $gender = "Female";
}
class Male extends Human {
    protected $gender = "Male";
}

虽然这行得通,但实际上并没有多大意义。 class 本身告诉你人类是什么性别,所以你可以调用 $human instanceof Female.

$person = new Female(18);
if ($person instanceof Female) {
    echo "Person is female";
}

只需覆盖构造函数:

class Human {
     public function __construct($age, $gender, $height, $weight) {
     }
}

class Female extends Human {
    public function __construct($age, $height, $weight) {
         parent::__construct($age, 'Female', $height, $weight);
    }
}