Home >Backend Development >PHP Tutorial >Understand the usage of static and const keywords in php5

Understand the usage of static and const keywords in php5

WBOY
WBOYOriginal
2016-07-25 09:04:36752browse
  1. class Counter
  2. {
  3. private static $count = 0; //Define a static property
  4. const VERSION = 2.0; //Define a constant
  5. //Constructor
  6. function __construct(){
  7. self::$count++;
  8. }
  9. //Destructor
  10. function __destruct(){
  11. self::$count--;
  12. }
  13. //Define a static method
  14. static function getCount(){
  15. return self ::$count;
  16. }
  17. }
  18. //Create an instance
  19. $c = new Counter();
  20. //Perform printing
  21. print( Counter::getCount(). "
    " ); //Use Directly enter the class name to access the static method Counter::getCount
  22. //Print the version of the class
  23. print( "Version used: " .Counter::VERSION. "
    " );
  24. ?>
Copy code


Statement:
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