## 構造函數和析構函數
### 構造函數
**\_\_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
?>
```
- 序言
- 簡介
- PHP是什么?
- PHP能做什么?
- 基本語法
- 類型
- boolean(布爾型)
- integer(整型)
- float(浮點型)
- string(字符串)
- array(數組)
- object(對象)
- callable(可調用)
- resource(資源)
- NULL(無類型)
- 偽類型
- 類型轉換的判別
- 變量
- 基礎
- 預定義變量
- 變量范圍
- 可變變量
- 來自PHP之外的變量
- 常量
- 語法
- 魔術常量
- 表達式
- 運算符
- 運算符優先級
- 算術運算符
- 賦值運算符
- 位運算符
- 比較運算符
- 錯誤控制運算符
- 執行運算符
- 遞增/遞減運算符
- 邏輯運算符
- 字符串運算符
- 數組運算符
- 類型運算符
- 流程控制
- if
- else
- elseif/else if
- 流程控制的替代語法
- while
- do-whille
- for
- foreach
- break
- continue
- switch
- declare
- return
- require
- include
- require_once
- include_once
- goto
- 函數
- 用戶自定義函數
- 函數的參數
- 返回值
- 可變函數
- 內部 (內置)函數
- 匿名函數
- 類與對象
- 簡介
- 基本概念
- 屬性
- 類的自動加載
- 構造函數
- 訪問控制(可見性)