0
父類是從子類外部構造的,因此,它的構造函數不能從子內部調用。在這種情況下,應該如何訪問孩子父母的屬性。從子類訪問外部初始化父類的屬性
實施例:在運行時被返回
class MyParent {
protected $args;
protected $child;
public function MyParent($args=false){
$this->args=$args;
$this->child=new MyChild();
}
public function main(){
$this->child->printArgs();
}
}
class MyChild extends MyParent{
public function MyChild(){}
public function printArgs(){
Echo "args: ".$this->args['key']." = ".$this->args['value']."\n";
}
}
$parent=new MyParent(array('key'=>'value'));
$parent->main();
空變量:
[email protected]:~/code/otest$ php run.php
args: =