Home  >  Article  >  Backend Development  >  php中const跟static的区别

php中const跟static的区别

WBOY
WBOYOriginal
2016-06-13 12:14:33788browse

php中const和static的区别

博主热衷各种互联网技术,常啰嗦,时常伴有强迫症,常更新,觉得文章对你有帮助的可以关注我。 转载请注明"深蓝的镰刀"


class Test{    static $test_var = 10;    const TEST_CONST = 5;}echo Test::$test_var;   //返回 10echo Test::MY_CONST;  // 返回 5Test::$test_var = 20;   echo Test::$test_var; //返回20Test::TEST_CONST = 20;  // 错误

实际上两者概念完全不同,但是有时候使用可能会混淆

static就是静态变量,遵循php变量命名规则,可以在类没有实例化的时候访问和修改。

而const是常量的意思,遵循常量命名规则(不可以使用$作为常量名),类似define,和define一样不允许被修改,但不同于define之处在于const变量可以放在类中以实现Test::TEST_CONST这样的访问方式。



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