Php constructor and Destructor analysis

Source: Internet
Author: User
Php constructor and Destructor analysis

  1. Class counter

  2. {
  3. Private static $ count = 0;

  4. Function _ construct ()

  5. {
  6. Self: $ count ++;
  7. }

  8. Function _ destruct ()

  9. {
  10. Self: $ count --;
  11. }

  12. Function getcount ()

  13. {
  14. Return self: $ count;
  15. }
  16. }

  17. // Create the first instance

  18. $ C = new counter ();

  19. // Output 1

  20. Print ($ c-> getcount ()."
  21. \ N ");

  22. // Create the second instance

  23. $ C2 = new counter ();

  24. // Output 2

  25. Print ($ c-> getcount ()."
  26. \ N ");

  27. // Destroy the instance

  28. $ C2 = null;

  29. // Output 1

  30. Print ($ c-> getcount ()."
  31. \ N ");
  32. ?>

Note: When a new instance is created, the memory is prepared to store all the attributes. each instance has its own unique set of attributes. However, the method is shared by all instances of this class.

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.