教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 深入分析php中const和define定义常量的区别

深入分析php中const和define定义常量的区别

发布时间:2016-10-15   编辑:jiaochengji.com
教程集为您提供深入分析php中const和define定义常量的区别等资源,欢迎您收藏本站,我们将为您提供最新的深入分析php中const和define定义常量的区别资源
const和define在php中都是定义常量了,但是它们的具体区别是什么?其实非常的简单const用于类成员变量定义,一旦定义且不能改变其值。define定义全局常量,在任何地方都可以访问 页define不能在类中定义而const可以了,下面整理了一篇文章。

大家都知道define是定义常量的,如果在类中定义常量呢?当然不能用define,而用const,如下例:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6718')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6718>
<?php
define('PHP', 'I love PHP'); // 在类外面通常这样定义常量
if (defined('PHP')) {
    echo 'PHP is defined!';
}
 
class MyClass
{
    // 常量的值将始终保持不变。在定义和使用常量的时候不需要使用$符号
    const CONSTANT = 'constant value';
 
    function showConstant() {
        echo self::CONSTANT . '<br/>';
    }
}
 
echo MyClass::CONSTANT . '<br/>';
 
$classname = 'MyClass';
echo $classname::CONSTANT . '<br/>'; // PHP 5.3.0 之后
 
$class = new MyClass();
$class->showConstant();
echo $class::CONSTANT.'<br/>'; // PHP 5.3.0 之后
 
print_r(get_defined_constants()); // 可以用get_defined_constants()获取所有定义的常量

一般是define在类外定义常量,const在类内定义常量,并且const必须通过类名::变量名来进行访问。但是php5.3以上支持类外通过const定义常量,看如下,这样是ok的:

 

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5812')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5812>const PHP_N = 'use const defined PHP';
echo PHP_N;
if (defined('PHP_N')) {
    echo 'PHP_N is defined!';
}

常量一般采用大写单词与下划线间隔的方式命名,常量必须通过 const 定义为类的成员,强烈不鼓励使用 define定义的全局常量。

关于常量的基础知识,这里不说了,除了以上,define和const的其它区别

1.const不能再条件语句中定义常量,但是define是可以的,如下:

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6725')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6725><?php
if (1) {
    const A = 'php';
}
echo A; // 必错

2.const采用一个普通的常量名称,define可以采用表达式作为名称

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2919')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2919>

 

<?php
const FOO = 'PHP';
for ($i = 0; $i < 32; $i) {
    define('PHP_' . $i, 1 << $i);
}

3.const只能接受静态的标量,而define可以采用任何表达式。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4605')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4605>
<?php
const PHP = 1 << 5; // 错误
define('JAVA', 1 << 5); // 正确
4.const本身就是一个语言结构。而define是一个函数。所以使用const速度要快的多。

 

常量的命名规范:

常量包含数字字母字符和下划线,数字允许作为常量名
采用大写单词与下划线间隔的方式,例如可以这样 EMBED_SUPPRESS_EMBED_EXCEPTION 但不许这样 EMBED_SUPPRESSEMBEDEXCEPTION
通常使用常量所在的包名或者是类名作为前缀。比如,对于Bug类中常量应该以BUG_开始
常量必须通过 “const” 定义为类的成员,强烈不鼓励使用 “define” 定义的全局常量

您可能感兴趣的文章:
深入分析PHP const与define使用区别
深入分析php中const和define定义常量的区别
php中const与define的区别分析
php常量定义的方式有哪些
php中define()与const的区别详解
const define 区别PHP
php中static,const与define的区别分析
PHP5.6 CONST新特性几个例子
详解PHP中const和define的区别
区别PHP中的const,static,public,private,protected

[关闭]
~ ~