解析PHP5析构函数的具体使用方法
在升级版的
在PHP5中,则使用__construct()来命名构造函数,而不再是与类同名,这样做的好处是可以使构造函数独立于类名,当类名改变时,不需要在相应的去修改构造函数的名称。
与构造函数相反,在PHP5中,可以定义一个名为__destruct()的函数,称之为PHP5析构函数,PHP将在对象在内存中被销毁前调用析构函数,使对象在彻底消失之前完成一些工作。对象在销毁一般可以通过赋值为null实现。
- <?php
- /*
- * Created on 2009-11-18
- *
- * To change the template for this generated file go to
- * Window - Preferences - PHPeclipse - PHP - Code Templates
- */
- class student{
- //属性
- private $no;
- private $name;
- private $gender;
- private $age;
- private static $count=0;
- function __construct($pname)
- {
- $this->name = $pname;
- self::$count++;
- }
- function __destruct()
- {
- self::$count--;
- }
- static function get_count()
- {
- return self::$count;
- }
- }
- $s1=new student(Tom);
- print(student::get_count());
- $s2=new student(jerry);
- print(student::get_count());
- $s1=NULL;
- print(student::get_count());
- $s2=NULL;
- print(student::get_count());
- ?>
上面这段代码就是PHP5析构函数的具体使用方法,希望对大家有所帮助。
本文地址:http://www.phprm.com/frame/php1003584.html
转载随意,但请附上文章地址:-)