自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

解析PHP5析構(gòu)函數(shù)的具體使用方法

開發(fā) 后端
我們?cè)趯W(xué)習(xí)PHP語(yǔ)言的時(shí)候,可能會(huì)發(fā)現(xiàn),它的構(gòu)造函數(shù)與析構(gòu)函數(shù)與以往有些許不同。比如PHP5析構(gòu)函數(shù)被定義為一個(gè)名為__destruct()的函數(shù)。

在升級(jí)版的PHP5中,都有構(gòu)造函數(shù)與PHP5析構(gòu)函數(shù)。但是在具體的實(shí)際操作中,他們的功能和使用方式已經(jīng)和普通的函數(shù)方式有所不同。每當(dāng)實(shí)例化一個(gè)類對(duì)象時(shí),都會(huì)自動(dòng)調(diào)用這個(gè)與類同名的函數(shù),使對(duì)象具有與生俱來的一些特征。

#t#在PHP5中,則使用__construct()來命名構(gòu)造函數(shù),而不再是與類同名,這樣做的好處是可以使構(gòu)造函數(shù)獨(dú)立于類名,當(dāng)類名改變時(shí),不需要在相應(yīng)的去修改構(gòu)造函數(shù)的名稱。

與構(gòu)造函數(shù)相反,在PHP5中,可以定義一個(gè)名為__destruct()的函數(shù),稱之為PHP5析構(gòu)函數(shù),PHP將在對(duì)象在內(nèi)存中被銷毀前調(diào)用析構(gòu)函數(shù),使對(duì)象在徹底消失之前完成一些工作。對(duì)象在銷毀一般可以通過賦值為null實(shí)現(xiàn)。

  1. <?php 
  2. /*  
  3.  * Created on 2009-11-18  
  4.  *  
  5.  * To change the template for this generated file go to  
  6.  * Window - Preferences - PHPeclipse - PHP - Code Templates  
  7.  */  
  8.  class student{  
  9.   //屬性  
  10.   private $no;  
  11.   private $name;  
  12.   private $gender;  
  13.   private $age;  
  14.     
  15.   private static $count=0;  
  16.   function __construct($pname)  
  17.   {  
  18.    $this->name = $pname;  
  19.    self::$count++;  
  20.   }  
  21.     
  22.   function __destruct()  
  23.   {  
  24.    self::$count--;  
  25.   }  
  26.     
  27.   static function get_count()  
  28.   {  
  29.    return self::$count;  
  30.   }  
  31.  }  
  32.    
  33.  $s1=new student("Tom");  
  34.  print(student::get_count());  
  35.    
  36.  $s2=new student("jerry");  
  37.  print(student::get_count());  
  38.    
  39.  $s1=NULL;  
  40.  print(student::get_count());  
  41.    
  42.  $s2=NULL;  
  43.  print(student::get_count());  
  44. ?> 

上面這段代碼就是PHP5析構(gòu)函數(shù)的具體使用方法,希望對(duì)大家有所幫助。

責(zé)任編輯:曹凱 來源: CSDN
相關(guān)推薦

2009-12-07 16:52:59

PHP函數(shù)getima

2009-11-24 16:28:41

PHP5魔術(shù)函數(shù)

2009-11-25 10:02:27

PHP會(huì)話Sessio

2009-11-26 19:05:04

PHP函數(shù)explod

2009-11-26 15:23:24

PHP函數(shù)ereg()

2009-12-01 19:02:20

PHP取整函數(shù)

2009-12-02 18:51:12

PHP分頁(yè)類

2009-11-30 15:00:19

PHP加密解密函數(shù)au

2009-11-18 18:33:23

Linux PHP5安

2009-11-24 19:25:32

PHP關(guān)聯(lián)數(shù)組

2009-12-01 17:00:49

PHP變量

2009-11-24 15:50:09

PHP上傳類uploa

2009-12-04 17:16:41

PHP析構(gòu)函數(shù)

2009-11-30 17:43:54

PHP split()

2009-12-01 18:02:41

PHP表單數(shù)組

2009-12-11 17:33:56

PHP5常用函數(shù)

2011-08-29 15:58:51

Lua函數(shù)

2009-11-23 19:33:12

PHP5多態(tài)性

2009-11-16 16:54:00

PHP構(gòu)造函數(shù)

2009-11-23 20:00:25

PHP5接口PHP5抽象類
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)