class Hello {
public function hi() {
echo "Hello, hi!\n";
}
}
class ParentClass {
public $obj;
public function __construct() {
$this->obj = new Hello;
}
}
class Test extends ParentClass {
public function __construct() {
$this->obj->hi();
}
}
$temp = new Test;
我得到的错误信息是“调用非成员函数hi()对象”。 $ obj应该引用类“Hello”的一个实例,但它显然不是 - 我做错了什么?PHP类属性引用对象
'$ OBJ != $ this-> obj' –
我只是在复制代码时忘了$ this。 – user3033490
我不知道,但可能是父类的构造函数是不会自动 – silentboy