Home >Backend Development >PHP Tutorial >Problem with $this in php static function
class A{
<code> 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 />'; }</code>
}
class B extends A{
<code> public $age = 22; private $money = 10; public function subtell(){ parent::tell(); echo $this->age,'<br />'; } public function subMoney() { parent::sayMoney(); echo $this->money,'<br />'; }</code>
}
$b = new B();
$b->subtell();//22 1 22;
echo '
The last sentence reports an error Using $this when not in object context
But when subMoney() is called, $this is not bound, $this points to the b object, and then executed parent::sayMoney(); is not bound to $this because it is called statically. Shouldn't it get 2000 when sayMoney() is executed? Why does it report an error? Is it different from the previous $b->subtell(); call? Same