const
- const是一个定义常量的关键字
- 用“const”修饰的成员属性的访问方式和“static”修饰的成员访问的方式差不多,也是使用“类名”,在方法里面使用“self”关键字。但是不用使用“$”符号,也不能使用对象来访问。
<?php
class MyClass{
//定义一个常量CONSTANT
const CONSTANT = 'constant value';
function showConstant() {
echo self::CONSTANT ; // 使用self访问,不要加“$”
}
}
echo MyClass::constant ; //使用类名来访问,也不加“$”
$class = new MyClass();
$class->showConstant();
// echo $class::constant; 是不允许的
?>
评论