class A{
public $age = 50;
private $money = 2000;
static public $head = 1;
public function tell(){
echo $this->age,'<br />';
echo self::$head,'<br />';
}
static public function sayMoney(){
echo $this->money,'<br />';
}}class B extends A{
public $age = 22;
private $money = 10;
public function subtell(){
parent::tell();
echo $this->age,'<br />';
}
public function subMoney()
{
parent::sayMoney();
echo $this->money,'<br />';
}}
$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();调用有啥不一样
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
静态方法内不能使用 this, 静态属性和方法被创建时,可能还没有任何这个类的实例可以被调用, 你可以classA::staticMethod() 或者 $a = new classA(); $a->staticMethod() , 但其内部就是不能使用this