码迷,mamicode.com
首页 > Web开发 > 详细

PHP $this->方法调用

时间:2015-06-16 13:13:04      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

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 $this->方法调用

标签:

原文地址:http://my.oschina.net/gilbertch/blog/467272

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!