在PHP中导入类和命名空间:领先的反斜杠有什么区别? [英] Importing classes and namespaces in PHP: What difference does a leading backslash make?

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

问题描述

这两者之间有什么区别:

What's the difference between those two:

use Exception;
use \Exception;

或者那些:

use Foo\Bar;
use \Foo\Bar;

手册说:


请注意,对于命名空间名称(完全$) b $ b限定的命名空间名称包含
命名空间分隔符,例如Foo \ Bar
而不是
的全局名称,例如FooBar),领先的
反斜杠是不允许使用
,因为导入名称必须完全符合
,并且相对于当前命名空间不会处理

Note that for namespaced names (fully qualified namespace names containing namespace separator, such as Foo\Bar 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 做同样的事情会产生同样的影响,但不会发出警告。)

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;

如果没有相对于当前命名空间处理导入名称,那么使用\ ArrayObject 使用ArrayObject 必须具有相同的含义。还有什么可以使用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.

回答你的问题:没有区别。但是,如果我是引擎开发人员,他也是无主导斜杠标准的信徒,那么我就不需要考虑有人写使用\ 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中导入类和命名空间:领先的反斜杠有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆