在PHP为什么不工作
问题描述:
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公告:未定义的变量:在d数据:\ test.php的第7行在PHP为什么不工作
PHP致命错误:在d无法访问空属性:\ test.php的第7行
答
更新您的功能someMethodInTheParentClass低于使用$这 - >数据= 123456;
public function someMethodInTheParentClass() {
$this->data = 123456;
}
+0
非常感谢〜! –
+0
欢迎好友,这是很好的帮助 –
答
Use `$this->data = 123456; `instead of` $this->$data = 123456;` in below function
public function someMethodInTheParentClass() {
$this->data = 123456;
}
答
MyParent和Child类中的构造方法是不必要的。
尝试函数:$ this->数据 –
使用$这 - >数据未$这 - > $数据 –
$这个 - >数据,而不是$这 - > $数据 –