能在类构造函数 PHP 中定义常量吗?


不,你不能在类构造函数中定义常量,你可以在类级别使用常量。

假设我们有如下类 −

class ConstantDemo
{ }

在类中定义一个常量 −

class ConstantDemo {
    const LANGUAGE_NAME="PHP";
} 

示例

 在线演示

<!DOCTYPE html>
<html>
<body>
<?php
class ConstantDemo {
   const LANGUAGE_NAME="PHP";
}
echo "The language Name is=",ConstantDemo::LANGUAGE_NAME;
?>
</body>
</html>

输出

The language Name is=PHP

更新于: 12-10-2020

688 次浏览

开启你的职业生涯

完成课程以获得认证

开始
广告
© . All rights reserved.