PHP 类中定义常量,并对常量的使用方法(转帖)

class MyClass
{
    const constant = 'constant value';

    function showConstant() {
        echo  self::constant . "\n";
    }
}

echo MyClass::constant . "\n";

$classname = "MyClass";
echo $classname::constant . "\n"; // PHP 5.3.0之后

$class = new MyClass();
$class->showConstant();

echo $class::constant."\n"; // PHP 5.3.0之后

你可能感兴趣的:(PHP,function,Class)