本文概述
- 常量是一种变量, 可以使用关键字const为任何类定义变量。
- 分配后, 这些变量的值将无法更改。
- 类常量与普通变量不同, 因为我们不需要$来声明类常量。
- 如果我们在类内部, 则可以使用self关键字获取常量的值, 但是要访问类外部的值, 则必须使用范围解析运算符。
例子1
<?php
//create class
class srcmini
{
//create constant variable
const a= "This is const keyword example";
}
//call constant variable.
echo srcmini::a;
?>
输出

例子2
<?php
//create class
class demo
{
//create constant variable
const a= 10;
}
//call constant variable.
echo demo::a;
?>
输出

来源:
https://www.srcmini02.com/29294.html