【发布时间】:2013-04-06 18:19:19
【问题描述】:
我的印象是子类继承了其父类的属性。但是,以下是 B 类中的输出 null... 有人可以告诉我如何访问父类中的属性吗?
$aClass = new A();
$aClass->init();
class A {
function init()
{
$this->something = 'thing';
echo $this->something; // thing
$bClass = new B();
$bClass->init();
}
}
class B extends A {
function init()
{
echo $this->something; // null - why isn't it "thing"?
}
}
【问题讨论】:
-
你定义了A类两次,B类根本没有……你确定这段代码是正确的吗?
-
你的意思是
class B extends A? -
对不起,这是一个错字,我更正了它......
标签: php class variables object