Home  >  Article  >  Backend Development  >  php类常量的使用详解_php技巧

php类常量的使用详解_php技巧

WBOY
WBOYOriginal
2016-05-17 09:02:36730browse
注意:不像其他的面向对象编程语言,在php中,类不能对某个属性变量使用final修饰符。
如果要声明某个属性为常量,可以使用const关键字,并且无需使用美元符号作为变量名前缀,也无需使用访问权限修饰符。常量意味着虽然可以访问该变量,但不能修改该变量的值。例如下边的代码声明了常量属性con_var:
复制代码 代码如下:

class Foo{
 const con_var="常量属性的值不能被修改
";
 public function method_a(){
  echo (self::con_var);
 }
}
echo(Foo::con_var);
$myFoo=new Foo();
echo ($myFoo->method_a());
?>

常量属性不能使用对象访问,仅能使用类访问,在类本体内,可以使用“self::常量名”,在类本体外可以使用“类名::常量名”。
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