Home  >  Article  >  Backend Development  >  PHP $this->方法调用

PHP $this->方法调用

WBOY
WBOYOriginal
2016-06-23 13:32:371340browse

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();

输出

fatherson

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
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