在thinkphp框架中_initialize()与__construct()方法使用区别

直接看不同代码输出结果,都是运行index控制器中的index方法,index控制器继承公共控制器common

方案一:

父类中:

<?php
namespace app\index\controller;
use think\Controller;

class Common extends Controller
{
    public function __construct(){
        echo 'father';
    }
}

子类中:

<?php
namespace app\index\controller;
use think\Controller;
class Index extends Common
{    

    public function __construct(){
        echo 'son';
    }

    public function index(){
    }

}

输出:son  

如果将子类改为:

public function __construct(){
        parent::__construct();
        echo 'son';
    }

    public function index(){
    }

输出:fatherson

得出结论:当父类、子类中都有构造函数,子类重载了父类的构造函数,不会执行父类。如果你要调用父类的话,那么要加上parent::__construct()

当我们把上述的构造方法改为thinkphp中的_initialize()方法时运行会发现:结果与前面的一致,若要执行父类的_initialize()方法,也需要使用这一句:

parent::_initialize()

方案二:

父类中:

public function __construct(){
        parent::__construct();
        echo 'father';
    }

子类中:

public function __construct(){
        
        echo 'son';
    }

public function index(){  
 }

输出:son

上面情况__construct()都换成_initialize()结果一样。

方案三:

父类中:

public function __construct(){
        parent::__construct();
        echo 'father';
    }

子类中:

public function index(){       
   }

输出:father

父类中:

public function __construct(){
        
        echo 'father';
    }

子类中:

public function index(){       
   }

输出:father

上面两张情况__construct()都换成_initialize()结果一样。

方案四:

父类中:

public function __construct(){
        echo 'father';
    }

子类中:

public function _initialize(){
        
        echo 'son';
    }

    public function index(){
    }

输出:father

父类中:

public function _initialize(){

        echo 'father';
    }

子类中:

public function __construct(){

        echo 'son';
    }

    public function index(){
          
    }

输出:son

得出结论:当THINKPHP的父类和子类中分别有构造函数__construct和初始化方法_initialize,THINKPHP不会去_initialize(),会执行__construct();

 

方案五:

父类中:

public function _initialize(){
        
        echo 'father';
    }

子类中:

public function __construct(){
        echo 'son1';
    }

    public function _initialize(){
        echo 'son2';
    }

    public function index(){

    }

输出:son1

父类中:

public function __construct(){
        
        echo 'father';
    }

子类中:

public function __construct(){
        echo 'son1';
    }

    public function _initialize(){
        echo 'son2';
    }

    public function index(){

    }

输出:son1

得出结论:当THINKPHP的子类中分别有构造函数__construct和初始化方法_initialize,THINKPHP不会去_initialize(),会执行__construct()

方案六:

父类中:

public function __construct(){
        if(method_exists($this,"hello")){
            $this->hello();
        }
        echo 'father';
    }

子类中:

public function _initialize(){

        echo 'son';
    }

    public function index(){
          
    }

    public function hello(){
          echo 'hello';
    }

输出:hellofather

父类中:

public function __construct(){
        if(method_exists($this,"hello")){
            $this->hello();
        }
        echo 'father';
    }

子类中:

public function __construct(){
        echo 'son';
    }

    public function index(){

    }

    public function hello(){
        echo 'hello';
    }

输出:son

猜你喜欢

转载自blog.csdn.net/sz80443374/article/details/83274879
今日推荐