解析PHP5解構函式的具體使用方法_PHP教程

來源:互聯網
上載者:User
在升級版的在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. private static $count=0;
  15. function __construct($pname)
  16. {
  17. $this->name = $pname;
  18. self::$count++;
  19. }
  20. function __destruct()
  21. {
  22. self::$count--;
  23. }
  24. static function get_count()
  25. {
  26. return self::$count;
  27. }
  28. }
  29. $s1=new student("Tom");
  30. print(student::get_count());
  31. $s2=new student("jerry");
  32. print(student::get_count());
  33. $s1=NULL;
  34. print(student::get_count());
  35. $s2=NULL;
  36. print(student::get_count());
  37. ?>

上面這段代碼就是PHP5解構函式的具體使用方法,希望對大家有所協助。


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

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.