Home >Backend Development >PHP Tutorial >Problem with $this in php static function

Problem with $this in php static function

WBOY
WBOYOriginal
2016-10-23 23:59:571074browse

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 '


';
$b->subMoney();

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn