私は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();
'Parents'クラスに' public function display_values() 'を追加するとどうなりますか? –
@u_mulder、もし私がそれをしたら、それは** $ this-> dob **のために何も印刷されません。 –