欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

理解php5中static和const关键字用法

程序员文章站 2024-01-16 13:28:28
...
  1. class Counter
  2. {
  3. private static $count = 0;//定义一个静态属性
  4. const VERSION = 2.0;//定义一个常量
  5. //构造函数
  6. function __construct(){
  7. self::$count++;
  8. }
  9. //析构函数
  10. function __destruct(){
  11. self::$count--;
  12. }
  13. //定义一个静态的方法
  14. static function getCount(){
  15. return self::$count;
  16. }
  17. }
  18. //创建一个实例
  19. $c = new Counter();
  20. //执行打印
  21. print( Counter::getCount(). "
    " ); //使用直接输入类名来访问静态方法Counter::getCount
  22. //打印类的版本
  23. print( "Version useed: " .Counter::VERSION. "
    " );
  24. ?>
复制代码