Thursday, May 17, 2007

PHP 常量的定义和使用

如果所定义的常量不在类内,则可以这样写(参考:http://www.phpe.net/manual/language.constants.php):

("CONSTANT", "Hello world.");
echo
CONSTANT; // outputs "Hello world."
echo Constant; // outputs "Constant" and issues a notice.


如果定义的常量在类内部,则应当且必须这样写(参考:http://i.92wy.com/personal_blog_show.aspx?id=39341

class MyClass
{ const constant = 'constant value';
function
showConstant() { echo self::constant."\n"; }
}
echo
MyClass::constant."\n";
$class = new MyClass();
$class->showConstant();// echo $class::constant; is not allowed

No comments: