问题描述
在 PHP 中,可以通过两种方式声明常量:
In PHP, you can declare constants in two ways:
带
define
关键字
define('FOO', 1);
使用 const
关键字
const FOO = 1;
- 这两者之间的主要区别是什么?
- 您何时以及为何应使用其中一种,何时使用另一种?
推荐答案
从 PHP 5.3 开始,有两种方法可以 define constants:使用 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
总是区分大小写,而 define()
允许您通过传递 true
作为定义不区分大小写的常量第三个参数(注意:定义不区分大小写的常量自 PHP 7.3.0 起已弃用,自 PHP 8.0.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 and removed since PHP 8.0.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
simply reads nicer. It's a language construct instead of a function and also is consistent with how you define constants in classes.
const
是一种语言结构,可以通过自动化工具进行静态分析.
const
, being a language construct, can be statically analysed by automated tooling.
const
在当前命名空间中定义一个常量,而 define()
必须传递完整的命名空间名称:
const
defines a constant in the current namespace, while define()
has to be passed the full namespace name:
namespace ABC;
// To define the constant ABCFOO:
const FOO = 'BAR';
define('ABCFOO', '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!
这篇关于PHP |define() 与 const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!