__construct 與 ThinkPhp _initialize 的區別

來源:互聯網
上載者:User

標籤:

一開始,不怎麼瞭解這個東西,所以最近想到了就來研究一下這個東西。

首先,我就先說說php中的繼承,__construct是類中的建構函式,用於執行個體化

 1 <?php 2     class Action{ 3         public function __construct(){ 4             echo ‘Action‘; 5         } 6     } 7  8     class SonAction extends Action{ 9         public function __construct(){10             echo ‘Son Action‘;11         }12     }13 14     $son = new SonAction();15 16     //result:Son Action17 ?>

在父類中定義建構函式,子類中使用建構函式,樣本化子類,輸出Son Action,沒有調用父類中的建構函式

<?php    class Action{        public function __construct(){            echo ‘Action‘;        }    }    class SonAction extends Action{        public function __construct(){            parent::__construct();            echo ‘Son Action‘;        }    }    $son = new SonAction();    //result:ActionSon Action?>

在子類中,使用parent::__construct(),調用父類的建構函式,因為繼承了父類,定義自己的構造方法,重載了__construct(),所以需要調用父類的建構函式,需要parent::__construct

<?php    class Action{        public function __construct(){            echo ‘Action‘;        }    }    class SonAction extends Action{    }    $son = new SonAction();    //result:Action?>

在繼承的子類中沒有自己的建構函式,可以說是繼承了父類的建構函式(個人理解,可以探討一下),所以執行個體化的時候,輸出得到result

//接下來我說說_initialize 這個函數

其實這個函數不是原生php含有的,而是一個TP自己定義的一個函數

我們可以再TP的架構上看到,我的版本是3.1.3

   /**     * 架構函數 取得模板對象執行個體     * @access public     */    public function __construct() {        tag(‘action_begin‘,$this->config);        //執行個體化視圖類        $this->view     = Think::instance(‘View‘);                   //控制器初始化        if(method_exists($this,‘_initialize‘))            $this->_initialize();    }

這個函數是Action中的建構函式,在控制器初始化中,使用了method_exists,判斷當前執行個體化的類中是否函數_initialize這個函數,

當子類沒有建構函式的時候,子類繼承父類的建構函式(子類會繼承[或調用最近父類的建構函式]),判斷子類中是否含有_initialize這個函數,

若含有,調用函數,在構造的時候調用該函數,在construct之後。。。。。

下面這個例子:可以仔細觀察出來

 Action類的構造器    public function __construct() {        echo  ‘grand father‘;        tag(‘action_begin‘,$this->config);        //執行個體化視圖類        $this->view     = Think::instance(‘View‘);                   //控制器初始化        if(method_exists($this,‘_initialize‘))            $this->_initialize();    } TestAction類的構造器    class TestAction extends Action{                public function __construct(){            echo ‘Test override Action‘;        }    }  SonatestAction類初始化函數    class SontestAction extends TestAction{        public function _initialize(){            echo ‘Son test‘;        }                public function index(){            echo 222;            die();        }    }  //輸出得到Test override Action222  

上面的例子得到結果分析可得到: TestAction重載了Action中的構造器,所以訪問SontestAction中的index的時候,首先會調用建構函式,則會調用父類的構造器,父類構造器中不含有調用_initialize所以。。。子類中沒有調用_initialize

哪裡有錯誤可以留言討論。

轉載請註明轉載地址,原創有毒,轉載送菊花:http://i.cnblogs.com/EditPosts.aspx?postid=4204198&update=1

__construct 與 ThinkPhp _initialize 的區別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.