我正在學習PHP,剛剛開始使用中間級別。下面是類層次結構 -在PHP中重載多級繼承中的函數,但沒有任何參數
FamilyTree
|
|
Grandparents
|
|
Parents
Parents
呼籲$dob
其餘額外的屬性從Grandparents
我能超載,因爲它們在參數的數目不同繼承的,我怎麼超載display_values()
因爲它沒有任何爭論?
這是代碼:
<?php
class FamilyTree {
protected $name;
protected $sex;
protected $married;
protected $number_of_kids;
public function display_values() {
echo $this->name . "<br />";
echo $this->sex . "<br />";
echo $this->married . "<br />";
echo $this->number_of_kids . "<br />";
}
}
class Grandparents extends FamilyTree {
public function set_values($name, $sex, $married, $number_of_kids) {
$this->name = $name;
$this->sex = $sex;
$this->married = $married;
$this->number_of_kids = $number_of_kids;
}
}
class Parents extends Grandparents {
private $dob;
public function __call($function_name, $arguments) {
if($function_name == 'set_values' && count($arguments) == 5) {
$this->$name = $name;
$this->sex = $sex;
$this->married = $married;
$this->number_of_kids = $number_of_kids;
$this->dob = $dob;
}
}
}
$gp = new Grandparents();
$gp->set_values("James", "Male", 'yes', 5);
$p = new Parents();
$p->set_values("Samuel", "Male", 'yes', 2, '2/5/1974');
$gp->display_values();
那麼如果您只是將'public function display_values()'添加到'Parents'類會發生什麼? –
@u_mulder,如果我這樣做,那麼它不會打印任何** $ this-> dob ** –