I'm reading a book about PHP, and about using TRUE
and FALSE
, it says :
我正在读一本关于PHP的书,关于使用TRUE和FALSE,它说:
In fact, the lowercase versions are more stable, because PHP does not allow you to redefine them; the uppercase ones may be redefined
实际上,小写版本更稳定,因为PHP不允许您重新定义它们;大写的可以重新定义
I tried to redefine TRUE
and FALSE
, and it didn't work!! I google redefining constants and found out that i need to use runkit_constant_redefine()
, i don't have runkit extension installed so i can't try it on TRUE and FALSE..
我试图重新定义TRUE和FALSE,但它没有用!!我谷歌重新定义常量,发现我需要使用runkit_constant_redefine(),我没有安装runkit扩展,所以我不能尝试TRUE和FALSE ..
My question is, Can TRUE
, true
, FALSE
or false
be redefined with or without runkit_constant_redefine()
?
我的问题是,是否可以使用或不使用runkit_constant_redefine()重新定义TRUE,true,FALSE或false?
4 个解决方案
#1
4
Boolean true is defined as case-insensitive constant, with true
being the default notation.
Boolean true定义为不区分大小写的常量,true为默认表示法。
define("true", 1, 1);
That means it will work in any other casing as well, be it TRUE
or True
or TrUe
or tRUE
.
这意味着它也适用于任何其他外壳,无论是TRUE还是True,还是TrUe或tRUE。
What your book alludes to is redefining the constant in another case variant again. Which you can. All but the lowercase true
are open spots in the constant lookup table.
你的书所暗示的是再次在另一个案例变体中重新定义常量。你可以。除小写字母以外的所有内容都是常量查找表中的开放点。
With e.g. define("True", 2)
it will take precedence over the lowercase-defined true
which would substitute for the other cases else.
用例如define(“True”,2)它将优先于小写定义的true,它将替代其他情况。
It's pointless advice from your book anyway. Even though you could declare a dozen variants for the boolean constants, nobody actually does that. The presumed "more stable" reasoning is practically bogus. Prefer the notation that's more readable or matches the existing coding style.
无论如何,这是你书中毫无意义的建议。即使您可以为布尔常量声明十几个变体,但实际上并没有人这样做。假定的“更稳定”的推理实际上是假的。更喜欢更符合现有编码风格的符号或符合现有编码风格的符号。
#2
0
Yes, you can, for the uppercase versions:
是的,你可以,对于大写版本:
$ php
php > var_dump(defined('TRUE'), TRUE);
bool(true)
bool(true)
php > define('TRUE', 'arglebargle');
php > var_dump(defined('TRUE'), TRUE);
bool(true)
string(11) "arglebargle"
php > echo phpversion();
5.4.16
Lowercase, not so much:
小写,而不是:
php > var_dump(defined('true'), true);
bool(true)
bool(true)
php > define('true', 'foobarbaz');
PHP Notice: Constant true already defined in php shell code on line 1
But why would you want to? Redefining reality rarely works out in the end.
但你为什么要这样?重新定义现实最终很难实现。
#3
0
You can define different true
and false
in every namespace.
您可以在每个命名空间中定义不同的true和false。
namespace foo;
define('foo\true', 0);
if (true) {
echo 'This will be never printed.';
}
#4
-1
Here's an excerpt from PHP's manual Booleans:
以下是PHP手册Booleans的摘录:
the constants TRUE or FALSE. Both are case-insensitive.
常量为TRUE或FALSE。两者都不区分大小写。
…
...
$foo = True; // assign the value TRUE to $foo
…
...
if ($show_separators == TRUE) {
As this is the wording of the manual you can assume that new releases likely won't break backward compatibility. You can consider writing True
in any case stable. Although using one consistent code style is recommended. But this has nothing to do with stability.
由于这是本手册的措辞,您可以假设新版本可能不会破坏向后兼容性。在任何情况下都可以考虑写入True。虽然建议使用一种一致的代码样式。但这与稳定性无关。
The fact that the case-insensitive constant is stored with the canonicalized lower case true
is an implementation detail and has no further relevance.
不区分大小写的常量与规范化的小写为真存储的事实是一个实现细节,没有进一步的相关性。
Regarding those who claim to redefine TRUE
: The only scenario I managed to do that was in an own namespace:
关于声称重新定义TRUE的人:我设法做的唯一场景是在自己的命名空间中:
namespace foo;
define("TRUE", "bar");
assert (TRUE === "bar");
assert (TRUE !== \TRUE)
This is defining foo\TRUE
, not \TRUE
!
这是定义foo \ TRUE,而不是\ TRUE!
Mario wrote, in pre-namespace PHP versions (PHP<5.3) redefining TRUE
was possible. Well, PHP-5.3 was released 5 years ago. Surprisingly the market share of PHP<5.3 is around 23% (of PHP-5). So I guess there is still valid relevance for this topic.
Mario写道,在命名空间之前的PHP版本(PHP <5.3)中,重新定义TRUE是可能的。好吧,PHP-5.3于5年前发布。令人惊讶的是,PHP <5.3的市场份额约为23%(PHP-5)。所以我猜这个话题仍然有效。
#1
4
Boolean true is defined as case-insensitive constant, with true
being the default notation.
Boolean true定义为不区分大小写的常量,true为默认表示法。
define("true", 1, 1);
That means it will work in any other casing as well, be it TRUE
or True
or TrUe
or tRUE
.
这意味着它也适用于任何其他外壳,无论是TRUE还是True,还是TrUe或tRUE。
What your book alludes to is redefining the constant in another case variant again. Which you can. All but the lowercase true
are open spots in the constant lookup table.
你的书所暗示的是再次在另一个案例变体中重新定义常量。你可以。除小写字母以外的所有内容都是常量查找表中的开放点。
With e.g. define("True", 2)
it will take precedence over the lowercase-defined true
which would substitute for the other cases else.
用例如define(“True”,2)它将优先于小写定义的true,它将替代其他情况。
It's pointless advice from your book anyway. Even though you could declare a dozen variants for the boolean constants, nobody actually does that. The presumed "more stable" reasoning is practically bogus. Prefer the notation that's more readable or matches the existing coding style.
无论如何,这是你书中毫无意义的建议。即使您可以为布尔常量声明十几个变体,但实际上并没有人这样做。假定的“更稳定”的推理实际上是假的。更喜欢更符合现有编码风格的符号或符合现有编码风格的符号。
#2
0
Yes, you can, for the uppercase versions:
是的,你可以,对于大写版本:
$ php
php > var_dump(defined('TRUE'), TRUE);
bool(true)
bool(true)
php > define('TRUE', 'arglebargle');
php > var_dump(defined('TRUE'), TRUE);
bool(true)
string(11) "arglebargle"
php > echo phpversion();
5.4.16
Lowercase, not so much:
小写,而不是:
php > var_dump(defined('true'), true);
bool(true)
bool(true)
php > define('true', 'foobarbaz');
PHP Notice: Constant true already defined in php shell code on line 1
But why would you want to? Redefining reality rarely works out in the end.
但你为什么要这样?重新定义现实最终很难实现。
#3
0
You can define different true
and false
in every namespace.
您可以在每个命名空间中定义不同的true和false。
namespace foo;
define('foo\true', 0);
if (true) {
echo 'This will be never printed.';
}
#4
-1
Here's an excerpt from PHP's manual Booleans:
以下是PHP手册Booleans的摘录:
the constants TRUE or FALSE. Both are case-insensitive.
常量为TRUE或FALSE。两者都不区分大小写。
…
...
$foo = True; // assign the value TRUE to $foo
…
...
if ($show_separators == TRUE) {
As this is the wording of the manual you can assume that new releases likely won't break backward compatibility. You can consider writing True
in any case stable. Although using one consistent code style is recommended. But this has nothing to do with stability.
由于这是本手册的措辞,您可以假设新版本可能不会破坏向后兼容性。在任何情况下都可以考虑写入True。虽然建议使用一种一致的代码样式。但这与稳定性无关。
The fact that the case-insensitive constant is stored with the canonicalized lower case true
is an implementation detail and has no further relevance.
不区分大小写的常量与规范化的小写为真存储的事实是一个实现细节,没有进一步的相关性。
Regarding those who claim to redefine TRUE
: The only scenario I managed to do that was in an own namespace:
关于声称重新定义TRUE的人:我设法做的唯一场景是在自己的命名空间中:
namespace foo;
define("TRUE", "bar");
assert (TRUE === "bar");
assert (TRUE !== \TRUE)
This is defining foo\TRUE
, not \TRUE
!
这是定义foo \ TRUE,而不是\ TRUE!
Mario wrote, in pre-namespace PHP versions (PHP<5.3) redefining TRUE
was possible. Well, PHP-5.3 was released 5 years ago. Surprisingly the market share of PHP<5.3 is around 23% (of PHP-5). So I guess there is still valid relevance for this topic.
Mario写道,在命名空间之前的PHP版本(PHP <5.3)中,重新定义TRUE是可能的。好吧,PHP-5.3于5年前发布。令人惊讶的是,PHP <5.3的市场份额约为23%(PHP-5)。所以我猜这个话题仍然有效。