zoukankan      html  css  js  c++  java
  • ThinkPHP中initialize和construct的不同

    ThinkPHP中initialize()和construct()这两个函数都可以理解为构造函数,前面一个是tp框架独有的,后面的是php构造函数,那么这两个有什么不同呢?

     

    在网上搜索,很多答案是两者是一样的,ThinkPHP中initialize相当于php的construct,这么说是错误的,如果这样,tp为什么不用construct,而要自己弄一个ThinkPHP版的initialize构造函数呢?

     

    自己试一下就知道两者的不同了。

     

    a.php

    class a{

        function __construct(){

            echo 'a';

        }

    }

    复制代码

    b.php(注意:这里构造函数没有调用parent::__construct();)

    include 'a.php';

    class b extends a{

        function __construct(){

            echo 'b';

        }

    }

     

    $test=new b();

    复制代码

    运行结果:

    b

    复制代码

    可见,虽然b类继承了a类,但是输出结果证明程序只是执行了b类的构造函数,而没有自动执行父类的构造函数。

     

    如果b.php的构造函数加上parent::__construct(),就不同了。

    include 'a.php';

    class b extends a{

        function __construct(){

            parent::__construct();

            echo 'b';

        }

    }

     

    $test=new b();

    复制代码

    那么输出结果是:

    ab

    复制代码

    此时才执行了父类的构造函数。

     

    我们再来看看thinkphp的initialize()函数。

     

    BaseAction.class.php

    class BaseAction extends Action{

        public function _initialize(){

                 echo 'baseAction';

        }

    复制代码

    IndexAction.class.php

    class IndexAction extends BaseAction{

        public function (){

                 echo 'indexAction';

            }

    复制代码

    运行Index下的index方法,输出结果:

    baseActionindexAcition

    复制代码

    可见,子类的_initialize方法自动调用父类的_initialize方法。而php的构造函数construct,如果要调用父类的方法,必须在子类构造函数显示调用parent::__construct();

     

    这就是ThinkPHP中initialize和construct的不同。

  • 相关阅读:
    快速幂取模算法详解
    牛客网小白月赛5I区间(差分数组)
    多重背包模板
    hdu5791(DP)
    CodeForces
    最长上升子序列LIS(51nod1134)
    POJ1088(记忆搜索加dp)
    最长公共子序列LCS(POJ1458)
    Gym 100971J-Robots at Warehouse
    模板
  • 原文地址:https://www.cnblogs.com/doseoer/p/5347045.html
Copyright © 2011-2022 走看看