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

Problem with $this in php static function

WBOY
Release: 2016-10-23 23:59:57
Original
1079 people have browsed it

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>
Copy after login

}
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>
Copy after login

}

$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

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template