class GrandClass {
public $data;
public function __construct() {
$this->someMethodInTheParentClass();
}
public function someMethodInTheParentClass() {
$this->$data = 123456;
}
}
class MyParent extends GrandClass{
public function __construct() {
parent::__construct();
}
}
class Child extends MyParent {
// public $data;
public function __construct() {
parent::__construct();
}
public function getData() {
return $this->data;
}
}
$a = new Child();
var_dump($a->getData());
PHP注意:未定义的变量:第7行的D:\ test.php中的数据
PHP致命错误:无法访问第7行的D:\ test.php中的空属性
答案 0 :(得分:4)
使用$ this-> data = 123456更新你的函数someMethodInTheParentClass:
public function someMethodInTheParentClass() {
$this->data = 123456;
}
答案 1 :(得分:2)
Use `$this->data = 123456; `instead of` $this->$data = 123456;` in below function
public function someMethodInTheParentClass() {
$this->data = 123456;
}