php 静态函数中$this的问题
程序员文章站
2022-04-25 23:20:39
...
class A{
';
$b->subMoney();
public $age = 50;
private $money = 2000;
static public $head = 1;
public function tell(){
echo $this->age,'
';
echo self::$head,'
';
}
static public function sayMoney(){
echo $this->money,'
';
}
}
class B extends A{
public $age = 22;
private $money = 10;
public function subtell(){
parent::tell();
echo $this->age,'
';
}
public function subMoney()
{
parent::sayMoney();
echo $this->money,'
';
}
}
$b = new B();
$b->subtell();//22 1 22;
echo '
';
$b->subMoney();
最后一句话报错Using $this when not in object context
但在调用subMoney()时不就绑定了$this,$this指向b对象,之后执行parent::sayMoney();由于静态调用,所以不绑定$this.在sayMoney()执行的时候不应该得到2000吗,为什么会报错,和前面$b->subtell();调用有啥不一样