PHP教程
PHP Mysql
PHP面向对象
PHP常用

PHP Const关键字

PHP Const关键字

常量是一种变量,我们可以用关键字 const 为任何类定义它。 这些变量的值在赋值后无法更改。 类常量与普通变量不同,因为我们不需要 $ 来声明类常量。 如果我们在类内部,则可以使用 self 关键字获取常量的值,但在类外部访问值则必须使用 作用域解析运算符。

示例 1

<?php
    //create class
    class lidihuo
    {
        //create constant variable
        const a= "this is const keyword example";
    }
//call constant variable.
echo lidihuo::a;
?>
输出:
Const keyword

示例 2

<?php
    //create class
    class demo
    {
        //create constant variable
        const a= 10;
    }
//call constant variable.
echo demo::a;
?>
输出:
Const keyword
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4