腾讯云618限量抢购
  1. PHP+MySQL数据库教程
  2. PHP进阶教程
  3. ThinkPHP 3.0/3.1 教程

PHP 常量(const)

最后更新:2011-01-06 16:38阅读:103478

常量 const

在类里面定义常量用 const 关键字,而不是通常的 define() 函数。

语法:

const constant = "value";

例子:

<?php
Class Person{
    // 定义常量
    const country = "中国";

    public function myCountry() {
        //内部访问常量
        echo "我是".self::country."人<br />";
    }
}

// 输出常量
echo Person::country."<br />";

// 访问方法
$p1 = new Person();
$p1 -> myCountry();
?>

运行该例子输出:

中国
我是中国人

常量的值一旦被定义后就不可在程序中更改。

参考阅读


本章节内容共分 17 部分:
  1. 1. PHP 类与对象
  2. 2. PHP 类的继承 extends 关键字
  3. 3. PHP 构造方法 __construct()
  4. 4. PHP 类的访问控制与封装 public,protected,private 修饰符
  5. 5. PHP 特殊方法 __set()、__get()、__isset() 与 __unset()
  6. 6. PHP 类的静态成员属性与静态方法 static 关键字
  7. 7. PHP 抽象方法与抽象类 abstract 关键字
  8. 8. PHP 对象克隆 clone 关键字与 __clone() 方法
  9. 9. PHP 类的接口 interface 与 implements 关键字
  10. 10. PHP 自动加载类 __autoload() 方法
  11. 11. PHP 对象的存储与传输(序列化 serialize 对象)
  12. 12. PHP 重载
  13. 13. PHP final 关键字
  14. 14. PHP 析构方法 __destruct()
  15. 15. 范围解析操作符(::)
  16. 16. PHP 常量(const)
  17. 17. PHP 重载方法 __call()
腾讯云618限量抢购