【发布时间】:2015-08-20 23:55:00
【问题描述】:
我创建了一个扩展类,以便为特定目的修改受保护的变量。但是我不明白如何从子类修改父类 protected var 并在父函数中的任何地方使用它。
例如:
class parent {
protected $data;
public function __construct() {
add_action('wp_ajax_output', array(&$this, 'output'));
}
public function output() {
get_data();
show();
}
public function get_data() {
$this->$data = 'data_1';
}
public function show() {
// here I'm using the protected var (I would like to use it from child)
echo $this->data;
}
}
new parent();
class child extends parent {
public function __construct() {
parent::__construct();
add_action('wp_ajax_child_output', array(&$this, 'child_output'));
}
public function child_output() {
$this->data = 'data_2';
// I would like to use $this->data in parent::show();
parent::show();
}
}
new child();
如何覆盖父级中所有受保护的 var 使用?
【问题讨论】:
-
实际上什么不起作用?
-
$this->当我运行子输出函数时,父 show() 函数中的数据为空
标签: php class variable-assignment extends