能在類建構函式 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.