php面向对象全攻略 (十) final static const关键字的使用

2019-04-10 06:14:07丽君

符号,也不能使用对象来访问。
代码片段

<?php
class MyClass{
//定义一个常量constant
const constant = 'constant value';
function showConstant() {
echo self::constant . "n"; //使用self访问,不要加”$”
}
}
echo MyClass::constant . "n"; //使用类名来访问,也不加”$”
$class = new MyClass();
$class->showConstant();
// echo $class::constant; 是不允许的
相关文章 大家在看