我想修改父项的$ item,然后让父项运行其余的进程。怎么做? (我无法修改通常通过软件更新更新的父类。另请注意,$ item是函数内部的变量,它不是父类 - > var;
Class Parent{
function __constructor(){....}
function save(){
$items = get_items($id, $this->var);
// process the data and save
}
}
Class Child extends Parent{
function __constructor(){
parent::__construct();
}
function save(){
$items = get_items($id, $var)// if.. then $var=$this->var, else $var=$something_else
// precess data and save
}
}
答案 0 :(得分:0)
将它们作为参数传递。例如
Class Parent{
function __constructor(){....}
function save(){
function save($items = null){
$items = is_null($items) ? get_items($id, $var) : $items;
// process the data and save
}
}
Class Child extends Parent{
function __constructor(){
parent::__construct();
}
function save($items = null){
$items = is_null($items) ? get_items($id, $var) : $items;
// precess data and save
}
return parent::save($items);
}
答案 1 :(得分:0)
Class Parent{
protected $items;
function __constructor(){....}
function save(){
$this->items = get_items($id, $this->var);
// process the data and save
}
}
Class Child extends Parent{
function __constructor(){
parent::__construct();
}
function save(){
$this->items = something_else();
parent::save();
}
}