在升級版的在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.bkjia.com/PHPjc/446387.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446387.htmlTechArticle在升級版的 在PHP5中,則使用__construct()來命名建構函式,而不再是與類同名,這樣做的好處是可以使建構函式獨立於類名,當類名改變時,...