与构造函数相反,在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.bkjia.com/phpjc/446387.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/446387.htmltecharticle在升级版的 在php5中,则使用__construct()来命名构造函数,而不再是与类同名,这样做的好处是可以使构造函数独立于类名,当类名改变时,...