PHP $this->方法调用
PHP $this->方法调用
ch-gilbert 发表于2年前
PHP $this->方法调用
  • 发表于 2年前
  • 阅读 141
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

摘要: $this-> will try to call private methods from the same scope

Reference: http://docs.php.net/manual/en/language.oop5.late-static-bindings.php

Note: In non-static contexts, the called class will be the class of the object instance. Since $this-> will try to call private methods from the same scope, using static:: may give different results. Another difference is that static:: can only refer to static properties.

Example #1

class Father
{
    public function __construct()
    {
        $this->init();
    }

    private function init()
    {
        echo 'father' . PHP_EOL;
    }
}

class Son extends Father
{
    public function __construct()
    {
        parent::__construct();
        $this->init();
    }
    
    private function init()
    {
        echo 'son' . PHP_EOL;
    }
}

new Son();

输出

father
son

Example #2

class Father
{
    public function __construct()
    {
        $this->init();
    }

    private function init()
    {
        echo 'father' . PHP_EOL;
    }
}

class Son extends Father
{  
    private function init()
    {
        echo 'son' . PHP_EOL;
    }
}

new Son();

输出

father
标签: PHP
共有 人打赏支持
粉丝 2
博文 3
码字总数 1347
×
ch-gilbert
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: