首頁  >  文章  >  後端開發  >  解析PHP5析构函数的具体使用方法_PHP教程

解析PHP5析构函数的具体使用方法_PHP教程

WBOY
WBOY原創
2016-07-15 13:29:11878瀏覽

在升级版的在PHP5中,则使用__construct()来命名构造函数,而不再是与类同名,这样做的好处是可以使构造函数独立于类名,当类名改变时,不需要在相应的去修改构造函数的名称。

与构造函数相反,在PHP5中,可以定义一个名为__destruct()的函数,称之为PHP5析构函数,PHP将在对象在内存中被销毁前调用析构函数,使对象在彻底消失之前完成一些工作。对象在销毁一般可以通过赋值为null实现。

  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析构函数的具体使用方法,希望对大家有所帮助。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446387.htmlTechArticle在升级版的 在PHP5中,则使用__construct()来命名构造函数,而不再是与类同名,这样做的好处是可以使构造函数独立于类名,当类名改变时,...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn