问题描述
在PHP中,何时使用
define('FOO', 1);
以及何时使用
const FOO = 1;
?
两者之间的主要区别是什么?
What are the main differences between those two?
推荐答案
从PHP 5.3开始,有两种方法定义常量:使用const
关键字或使用 define()
函数:
As of PHP 5.3 there are two ways to define constants: Either using the const
keyword or using the define()
function:
const FOO = 'BAR';
define('FOO', 'BAR');
这两种方式之间的根本区别是const
在编译时定义常量,而define
在运行时定义常量.这导致了大多数const
的缺点. const
的一些缺点是:
The fundamental difference between those two ways is that const
defines constants at compile time, whereas define
defines them at run time. This causes most of const
's disadvantages. Some disadvantages of const
are:
-
const
不能用于有条件地定义常量.要定义全局常量,必须在最外面的范围内使用它:
const
cannot be used to conditionally define constants. To define a global constant, it has to be used in the outermost scope:
if (...) {
const FOO = 'BAR'; // Invalid
}
// but
if (...) {
define('FOO', 'BAR'); // Valid
}
您为什么仍要这样做?一种常见的应用是检查常量是否已经定义:
Why would you want to do that anyway? One common application is to check whether the constant is already defined:
if (!defined('FOO')) {
define('FOO', 'BAR');
}
const
接受静态标量(数字,字符串或其他常量,例如true
,false
,null
,__FILE__
),而define()
接受任何表达式.由于const
中也允许使用PHP 5.6常量表达式:
const
accepts a static scalar (number, string or other constant like true
, false
, null
, __FILE__
), whereas define()
takes any expression. Since PHP 5.6 constant expressions are allowed in const
as well:
const BIT_5 = 1 << 5; // Valid since PHP 5.6 and invalid previously
define('BIT_5', 1 << 5); // Always valid
const
采用简单的常量名称,而define()
接受任何表达式作为名称.这样可以执行以下操作:
const
takes a plain constant name, whereas define()
accepts any expression as name. This allows to do things like this:
for ($i = 0; $i < 32; ++$i) {
define('BIT_' . $i, 1 << $i);
}
const
s始终区分大小写,而define()
允许您通过将true
用作第三个参数来定义不区分大小写的常量(注意:自PHP 7.3起,不区分大小写的常量的定义. 0.):
const
s are always case sensitive, whereas define()
allows you to define case insensitive constants by passing true
as the third argument (Note: defining case-insensitive constants is deprecated as of PHP 7.3.0.):
define('FOO', 'BAR', true);
echo FOO; // BAR
echo foo; // BAR
所以,那是不好的一面.现在,让我们看看除非出现以下情况之一,否则我个人始终使用const
的原因:
So, that was the bad side of things. Now let's look at the reason why I personally always use const
unless one of the above situations occurs:
-
const
读起来更好.它是一种语言构造而不是函数,并且与您在类中定义常量的方式保持一致. -
const
是一种语言构造,可以通过自动化工具进行静态分析. -
const
在当前名称空间中定义一个常量,而define()
必须传递完整的名称空间名称:
const
simply reads nicer. It's a language construct instead of a function and also is consistent with how you define constants in classes.const
, being a language construct, can be statically analysed by automated tooling.const
defines a constant in the current namespace, whiledefine()
has to be passed the full namespace name:
namespace A\B\C;
// To define the constant A\B\C\FOO:
const FOO = 'BAR';
define('A\B\C\FOO', 'BAR');
因为PHP 5.6 const
常量也可以是数组,而define()
尚不支持数组.但是,在PHP 7中,两种情况都将支持数组.
Since PHP 5.6 const
constants can also be arrays, while define()
does not support arrays yet. However, arrays will be supported for both cases in PHP 7.
const FOO = [1, 2, 3]; // Valid in PHP 5.6
define('FOO', [1, 2, 3]); // Invalid in PHP 5.6 and valid in PHP 7.0
最后,请注意,也可以在类或接口中使用const
来定义类常量或接口常量. define
不能用于此目的:
Finally, note that const
can also be used within a class or interface to define a class constant or interface constant. define
cannot be used for this purpose:
class Foo {
const BAR = 2; // Valid
}
// But
class Baz {
define('QUX', 2); // Invalid
}
摘要
除非您需要任何类型的条件或表达式定义,否则请使用const
s代替define()
s-仅仅是为了便于阅读!
Unless you need any type of conditional or expressional definition, use const
s instead of define()
s - simply for the sake of readability!
这篇关于define()与const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!