沃梦达 / 编程问答 / php问题 / 正文

在 PHP 中导入类和命名空间:前导反斜杠有什么区别?

Importing classes and namespaces in PHP: What difference does a leading backslash make?(在 PHP 中导入类和命名空间:前导反斜杠有什么区别?)

本文介绍了在 PHP 中导入类和命名空间:前导反斜杠有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两者有什么区别:

use Exception;
use Exception;

或者那些:

use FooBar;
use FooBar;

手册 说:

请注意,对于命名空间名称(完全限定的命名空间名称包含命名空间分隔符,例如 FooBar与全局名称相反不是,例如 FooBar),领先的反斜杠是不必要的,而不是允许,因为导入名称必须完整合格,不处理相对于当前命名空间.

Note that for namespaced names (fully qualified namespace names containing namespace separator, such as FooBar as opposed to global names that do not, such as FooBar), the leading backslash is unnecessary and not allowed, as import names must be fully qualified, and are not processed relative to the current namespace.

但我真的不明白这一点,因为上述所有变体都有效,即绝对不是不允许"的.

But I don't really understand this, as all of the above variants work, i.e. it definitely is not "not allowed".

查看 zend_do_use 表明,is_global(设置,当有前导反斜杠时)仅用于以下情况的警告:

A look into zend_do_use showed, that is_global (set, when there is a leading backslash) is only used for a warning in the following case:

namespace {
    use Exception;
}

这告诉我:具有非复合名称 'Exception' 的 use 语句无效".(虽然对 use Exception 做同样的事情会产生同样的影响,但不会发出警告.)

Which tells me: "The use statement with non-compound name 'Exception' has no effect". (Though doing the same with use Exception would have just as little effect, but does not throw a warning.)

所以:我错过了什么吗?真的有区别吗?

So: Am I missing something? Is there actually some difference?

推荐答案

手册将反斜杠指定为不必要,这自然意味着如果您仍然使用它,则其含义是等效的.但是,正如您所指出的,手册上说这是不允许的,这是错误的.

The manual specifies the backslash as unnecessary, which naturally means that if you still use it that the meaning is equivalent. However, as you have pointed out, the manual says that it is supposedly not allowed, which is false.

但是,手册还有其他问题.他们做广告:

However, there is something else troubling with the manual. They advertise this:

// importing a global class
use ArrayObject;

如果导入名称确实没有相对于当前命名空间进行处理,那么 use ArrayObjectuse ArrayObject 必须具有相同的含义.除了全局对象之外,use ArrayObject 还能引用什么?在实践中,引擎会导入全局的.

If it is true that import names are not processed relative to the current namespace, then use ArrayObject and use ArrayObject must have the same meaning. What else could use ArrayObject refer to other than the global one? In practice, the engine will import the global one.

此外,还有这样的错误:http://bugs.php.net/bug.php?id=49143

Also, with bugs such as this: http://bugs.php.net/bug.php?id=49143

我认为对于标准应该是什么存在混淆.

I believe there is confusion over what the standard is supposed to be.

回答你的问题:没有区别.但是,如果我是引擎开发者,同时也是无前导斜杠标准的信徒,那么我就不需要考虑有人写use Exception; 的情况.我相信这很可能是这种情况.

To answer your question: there is no difference. However, if I was the engine developer who was also a believer of the no-leading-slash standard, then I wouldn't need to consider a case where someone wrote use Exception;. I believe this was likely the case.

这篇关于在 PHP 中导入类和命名空间:前导反斜杠有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 PHP 中导入类和命名空间:前导反斜杠有什么区别?