PHP constructor and destructor analysis

WBOY
Release: 2016-07-25 08:53:50
Original
1068 people have browsed it
  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 a second instance

  23. $c2 = new counter();

  24. print($c->getcount() . "
  25. n");

  26. //Destroy the instance

  27. $c2 = null;

  28. < ;p> //Output 1
  29. print($c->getcount() . "
  30. n");
  31. ?>

Copy code

Note: When a new instance is created, memory is prepared to store all properties. Each instance has its own unique set of properties. But methods are shared by all instances of the class.



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!