【问题标题】:Why can you overwrite true but not false in namespaces? [closed]为什么你可以在命名空间中覆盖 true 但不能覆盖 false? [关闭]
【发布时间】:2012-11-30 13:11:07
【问题描述】:

根据我对http://3v4l.org/ZCJWA 的测试,以下示例(对于 PHP 5.3.10 - 5.4.6):

<?php
namespace Foo;
define('Foo\\true', false);
define('Foo\\false', true);

var_dump(
    true,
    false,
    1 === 1,
    1 === 0
);

将返回:

bool(false)
bool(false)
bool(true)
bool(false)

为什么你可以用false 覆盖true 而不能用true 覆盖false

【问题讨论】:

  • 因为true现在也是false,false被false覆盖了?

标签: php constants


【解决方案1】:

在您的第一个define 之后,true 现在被定义为false,所以Foo\\false 被设置为false

要使其按预期工作,您应该将 Foo\\trueFoo\\false 分别设置为 global spacetruefalse

define('Foo\\true', \false);
define('Foo\\false', \true);

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2010-10-03
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多