How to parse PHP5 destructor

Source: Internet
Author: User

In the upgraded versionIn PHP5, _ construct () is used to name the constructor, instead of the same name as the class. The advantage of this is that the constructor can be independent of the class name, when the class name changes, you do not need to modify the name of the constructor.

Opposite to constructor, in PHP5, you can define a function named _ destruct (), called PHP5 destructor, PHP will call the Destructor before the object is destroyed in the memory, so that the object can complete some work before it completely disappears. Objects can be destroyed by null.

 
 
  1. <?Php 
  2. /*
  3. * Created on 2009-11-18
  4. *
  5. * To change the template for this generated file go
  6. * Window-Preferences-PHPeclipse-PHP-Code Templates
  7. */
  8. Class student {
  9. // Attributes
  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=NewStudent ("Tom ");
  30. Print (student: get_count ());
  31. $S2=NewStudent ("jerry ");
  32. Print (student: get_count ());
  33. $S1=NULL;
  34. Print (student: get_count ());
  35. $S2=NULL;
  36. Print (student: get_count ());
  37. ?> 

The above code is the specific usage of the PHP5 destructor. I hope it will be helpful to you.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.