PHP鏋勯犲嚱鏁板拰鏋愭瀯鍑芥暟


鏋勯犲嚱鏁,void __construct ([ mixed $args [, $... ]] )
PHP 5 鍏佽寮鍙戣呭湪涓涓被涓畾涔変竴涓柟娉曚綔涓烘瀯閫犲嚱鏁般傚叿鏈夋瀯閫犲嚱鏁扮殑绫讳細鍦ㄦ瘡娆″垱寤哄璞℃椂鍏堣皟鐢ㄦ鏂规硶锛屾墍浠ラ潪甯搁傚悎鍦ㄤ娇鐢ㄥ璞′箣鍓嶅仛涓浜涘垵濮嬪寲宸ヤ綔銆

鏋勯犲嚱鏁

void __construct ([ mixed $args [, $... ]] )

PHP 5 鍏佽寮鍙戣呭湪涓涓被涓畾涔変竴涓柟娉曚綔涓烘瀯閫犲嚱鏁般傚叿鏈夋瀯閫犲嚱鏁扮殑绫讳細鍦ㄦ瘡娆″垱寤哄璞℃椂鍏堣皟鐢ㄦ鏂规硶锛屾墍浠ラ潪甯搁傚悎鍦ㄤ娇鐢ㄥ璞′箣鍓嶅仛涓浜涘垵濮嬪寲宸ヤ綔銆


Note: 濡傛灉瀛愮被涓畾涔変簡鏋勯犲嚱鏁板垯涓嶄細鏆椾腑璋冪敤鍏剁埗绫荤殑鏋勯犲嚱鏁般傝鎵ц鐖剁被鐨勬瀯閫犲嚱鏁帮紝闇瑕佸湪瀛愮被鐨勬瀯閫犲嚱鏁颁腑璋冪敤 parent::__construct()銆

Example #1 浣跨敤鏂版爣鍑嗙殑鏋勯犲嚱鏁
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
class BaseClass {
   function __construct() {
       print "In BaseClass constructor ";
   }
}
 
class SubClass extends BaseClass {
   function __construct() {
       parent::__construct();
       print "In SubClass constructor ";
   }
}
 
$obj = new BaseClass();
$obj = new SubClass();
?>

涓轰簡瀹炵幇鍚戝悗鍏煎鎬э紝濡傛灉 PHP 5 鍦ㄧ被涓壘涓嶅埌 __construct() 鍑芥暟锛屽畠灏变細灏濊瘯瀵绘壘鏃у紡鐨勬瀯閫犲嚱鏁帮紝涔熷氨鏄拰绫诲悓鍚嶇殑鍑芥暟銆傚洜姝ゅ敮涓浼氫骇鐢熷吋瀹规ч棶棰樼殑鎯呭喌鏄細绫讳腑宸叉湁涓涓悕涓 __construct() 鐨勬柟娉曪紝浣嗗畠鍗村張涓嶆槸鏋勯犲嚱鏁般
 

鏋愭瀯鍑芥暟

void __destruct ( void )

PHP 5 寮曞叆浜嗘瀽鏋勫嚱鏁扮殑姒傚康锛岃繖绫讳技浜庡叾瀹冮潰鍚戝璞$殑璇█锛屽 C++銆傛瀽鏋勫嚱鏁颁細鍦ㄥ埌鏌愪釜瀵硅薄鐨勬墍鏈夊紩鐢ㄩ兘琚垹闄ゆ垨鑰呭綋瀵硅薄琚樉寮忛攢姣佹椂鎵ц銆

 

Example #2 鏋愭瀯鍑芥暟绀轰緥

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<code><?php
class MyDestructableClass {
   function __construct() {
       print "In constructor ";
       $this->name = "MyDestructableClass";
   }
 
   function __destruct() {
       print "Destroying " . $this->name . " ";
   }
}
 
$obj = new MyDestructableClass();
?></code>

鍜屾瀯閫犲嚱鏁颁竴鏍凤紝鐖剁被鐨勬瀽鏋勫嚱鏁颁笉浼氳寮曟搸鏆椾腑璋冪敤銆傝鎵ц鐖剁被鐨勬瀽鏋勫嚱鏁帮紝蹇呴』鍦ㄥ瓙绫荤殑鏋愭瀯鍑芥暟浣撲腑鏄惧紡璋冪敤 parent::__destruct()銆
 


Note:

鏋愭瀯鍑芥暟鍦ㄨ剼鏈叧闂椂璋冪敤锛屾鏃舵墍鏈夌殑澶翠俊鎭凡缁忓彂鍑恒

Note:

璇曞浘鍦ㄦ瀽鏋勫嚱鏁颁腑鎶涘嚭涓涓紓甯镐細瀵艰嚧鑷村懡閿欒銆

浠ヤ笂灏辨槸PHP鏋勯犲嚱鏁板拰鏋愭瀯鍑芥暟鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒



鏈枃URL锛http://www.odweb.cn/news_show.html?id=100