class constants

Constants differ from normal variables in that you don't use the $ symbol to declare or use them.

 

<?php
class MyClass
{
    const constant = 'constant value';

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

echo MyClass::constant . "\n";

$classname = "MyClass";
echo $classname::constant . "\n"; // 自 5.3.0 起

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

echo $class::constant."\n"; // 自 PHP 5.3.0 起
?>

 

posted @ 2015-05-04 14:39  Mirrorhanman  阅读(131)  评论(0编辑  收藏  举报