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

Problem with $this in php static function

WBOY
WBOYOriginal
2016-10-24 00:12:421682browse

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

Reply content:

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

This cannot be used in static methods. When static properties and methods are created, there may not be any instances of this class that can be called. You can classA::staticMethod() or $a = new classA(); $a->staticMethod(), but it cannot be used internallythis

Wrong

The person above is right, the second function called uses static, so $this cannot be used

Here is the original answer

private indicates that it is private and only allows access within the class, even if it is an inherited class.

If inherited access is allowed and you don’t want external access, you can change it to: protected.

In addition, the instance of that class is created, and $this represents which class.

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