PHP头条
热点:

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


在升级版的PHP5中,都有构造函数与PHP5析构函数。但是在具体的实际操作中,他们的功能和使用方式已经和普通的函数方式有所不同。每当实例化一个类对象时,都会自动调用这个与类同名的函数,使对象具有与生俱来的一些特征。

  • PHP通用文件上传类的具体解析
  • PHP文件上传进度条的具体实现方式
  • 为你讲解PHP给图片加水印的具体方法
  • PHP上传类upload.php的具体使用方法
  • 如何运用PHP Ajax实现图片的无刷新上传
在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.phpzy.comtrue/php/15448.htmlTechArticle解析PHP5析构函数的具体使用方法 在升级版的PHP5中,都有构造函数与PHP5析构函数。但是在具体的实际操作中,他们的功能和使用方式已经和普通的函数方式有所不同。每当实例化一个类...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐