Home > Backend Development > PHP Tutorial > php中const和static的区别

php中const和static的区别

WBOY
Release: 2016-06-23 13:40:35
Original
1057 people have browsed it

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;  // 错误
Copy after login

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

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

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



Related labels:
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