<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 構造函數和析構函數 ### 構造函數 **\_\_construct** (\[ [mixed](https://www.php.net/manual/zh/language.pseudo-types.php#language.types.mixed) `$args` \[, `$...` \]\] ) : void PHP 5允許開發者在一個類中定義一個方法作為構造函數。具有構造函數的類會在每次創建新對象時先調用此方法,所有非常適合在使用對象之前做一些初始化工作。 > **Note**: 如果子類中定義了構造函數則不會隱式調用其父類的構造函數。要執行父類的構造函數,需要在子類的構造函數中調用 **parent::\_\_construct()**。如果子類沒有定義構造函數則會如同一個普通的類方法一樣從父類繼承(假如沒有被定義為 private 的話)。 **Example #1 使用新標準的構造函數** ``` <?php class?BaseClass?{ ???function?__construct()?{ ???????print?"In?BaseClass?constructor\n"; ???} } class?SubClass?extends?BaseClass?{ ???function?__construct()?{ ???????parent::__construct(); ???????print?"In?SubClass?constructor\n"; ???} } class?OtherSubClass?extends?BaseClass?{ ????//?inherits?BaseClass's?constructor } //?In?BaseClass?constructor $obj?=?new?BaseClass(); //?In?BaseClass?constructor //?In?SubClass?constructor $obj?=?new?SubClass(); //?In?BaseClass?constructor $obj?=?new?OtherSubClass(); ?> ``` 為了實現向后兼容性,如果 PHP 5在類中找不到[\_\_construct()](https://www.php.net/manual/zh/language.oop5.decon.php#object.construct)函數并且也沒有從父類繼承一個的話,它就會嘗試尋找舊式的構造函數,也就是和類同名函數。因此唯一會產生兼容性問題的情況是:類中已有一個名為 __construct() 的方法卻被用于其它用途時。 與其它方法不同,當 [\_\_construct()](https://www.php.net/manual/zh/language.oop5.decon.php#object.construct) 被與父類 [\_\_construct()](https://www.php.net/manual/zh/language.oop5.decon.php#object.construct) 具有不同參數的方法覆蓋時,PHP 不會產生一個 **`E_STRICT`** 錯誤信息。 自 PHP 5.3.3 起,在命名空間中,與類名同名的方法不再作為構造函數。這一改變不影響不在命名空間中的類。 **Example #2 Constructors in namespaced classes** ``` <?php namespace?Foo; class?Bar?{ ????public?function?Bar()?{ ????????//?treated?as?constructor?in?PHP?5.3.0-5.3.2 ????????//?treated?as?regular?method?as?of?PHP?5.3.3 ????} } ?> ``` ### 析構函數 **\_\_destruct** ( void ) : void PHP 5 引入了析構函數的概念, 這類似于其他面向對象的語言,如 C++ 。 析構函數會在到某個對象的所有引用都被刪除或者當對象被顯式銷毀時執行。 **Example #3 析構函數示例** ``` <?php class?MyDestructableClass?{ ???function?__construct()?{ ???????print?"In?constructor\n"; ???????$this->name?=?"MyDestructableClass"; ???} ???function?__destruct()?{ ???????print?"Destroying?"?.?$this->name?.?"\n"; ???} } $obj?=?new?MyDestructableClass(); ?> ``` 和構造函數一樣, 父類析構函數不會被引擎暗中調用。要執行父類的析構函數, 必須在子類的析構函數體中顯式調用`parent::destruct()`。 此外也和構造函數一樣,子類如果自己沒有定義析構函數則會繼承父類的。 析構函數即使在使用[exit()](https://www.php.net/manual/zh/function.exit.php)終止腳本運行時也會被調用。在析構函數中調用[exit()](https://www.php.net/manual/zh/function.exit.php)將會中止其余關閉操作的運行。 > **Note** > 析構函數在腳本關閉時調用,此時所有的 HTTP 頭信息已經發出。腳本關閉時的工作目錄有可能和在 SAPI(如 apache)中時不同。 > **Note** > 試圖在析構函數(在腳本終止時被調用)中拋棄一個異常會導致致命錯誤。 [***rayro at gmx dot de***](https://www.php.net/manual/zh/language.oop5.decon.php#99903) [?](https://www.php.net/manual/zh/language.oop5.decon.php#99903) **8 years ago** the easiest way to use and understand multiple constructors: ``` <?php class A { ? ? function __construct() ? ? { $a = func_get_args(); $i = func_num_args(); ? ? ? ? if (method_exists($this,$f='__construct'.$i)) { call_user_func_array(array($this,$f),$a); ? ? ? ? } ? ? } ? ? ? ? function __construct1($a1) ? ? { ? ? ? ? echo('__construct with 1 param called: '.$a1.PHP_EOL); ? ? } ? ? ? ? function __construct2($a1,$a2) ? ? { ? ? ? ? echo('__construct with 2 params called: '.$a1.','.$a2.PHP_EOL); ? ? } ? ? ? ? function __construct3($a1,$a2,$a3) ? ? { ? ? ? ? echo('__construct with 3 params called: '.$a1.','.$a2.','.$a3.PHP_EOL); ? ? } } $o = new A('sheep'); $o = new A('sheep','cat'); $o = new A('sheep','cat','dog'); // results: // __construct with 1 param called: sheep // __construct with 2 params called: sheep,cat // __construct with 3 params called: sheep,cat,dog ?> ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看