嗨,我有一个关于$ this的问题.
class foo {
function __construct(){
$this->foo = 'bar';
}
}
class bar extends foo {
function __construct() {
$this->bar = $this->foo;
}
}
Run Code Online (Sandbox Code Playgroud)
将
$ob = new foo();
$ob = new bar();
echo $ob->bar;
Run Code Online (Sandbox Code Playgroud)
结果bar??
我只是问,因为我认为它会,但我的脚本的一部分似乎没有导致我的想法.
PHP有点奇怪,如果定义子构造函数,则不会自动调用父构造函数 - 您必须自己调用它.因此,要获得您想要的行为,请执行此操作
class bar extends foo {
function __construct() {
parent::__construct();
$this->bar = $this->foo;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4137 次 |
| 最近记录: |