問(wèn)題描述
這兩者有什么區(qū)別:
use Exception;
use Exception;
或者那些:
use FooBar;
use FooBar;
手冊(cè) 說(shuō):
請(qǐng)注意,對(duì)于命名空間名稱(完全限定的命名空間名稱包含命名空間分隔符,例如 FooBar與全局名稱相反不是,例如 FooBar),領(lǐng)先的反斜杠是不必要的,而不是允許,因?yàn)閷?dǎo)入名稱必須完整合格,不處理相對(duì)于當(dāng)前命名空間.
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.
但我真的不明白這一點(diǎn),因?yàn)樯鲜鏊凶凅w都有效,即絕對(duì)不是不允許"的.
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
(設(shè)置,當(dāng)有前導(dǎo)反斜杠時(shí))僅用于以下情況的警告:
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;
}
這告訴我:具有非復(fù)合名稱 'Exception' 的 use 語(yǔ)句無(wú)效".(雖然對(duì) use Exception
做同樣的事情會(huì)產(chǎn)生同樣的影響,但不會(huì)發(fā)出警告.)
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.)
所以:我錯(cuò)過(guò)了什么嗎?真的有區(qū)別嗎?
So: Am I missing something? Is there actually some difference?
推薦答案
手冊(cè)將反斜杠指定為不必要,這自然意味著如果您仍然使用它,則其含義是等效的.但是,正如您所指出的,手冊(cè)上說(shuō)這是不允許的,這是錯(cuò)誤的.
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.
但是,手冊(cè)還有其他問(wèn)題.他們做廣告:
However, there is something else troubling with the manual. They advertise this:
// importing a global class
use ArrayObject;
如果導(dǎo)入名稱確實(shí)沒(méi)有相對(duì)于當(dāng)前命名空間進(jìn)行處理,那么 use ArrayObject
和 use ArrayObject
必須具有相同的含義.除了全局對(duì)象之外,use ArrayObject
還能引用什么?在實(shí)踐中,引擎會(huì)導(dǎo)入全局的.
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.
此外,還有這樣的錯(cuò)誤:http://bugs.php.net/bug.php?id=49143
Also, with bugs such as this: http://bugs.php.net/bug.php?id=49143
我認(rèn)為對(duì)于標(biāo)準(zhǔn)應(yīng)該是什么存在混淆.
I believe there is confusion over what the standard is supposed to be.
回答你的問(wèn)題:沒(méi)有區(qū)別.但是,如果我是引擎開發(fā)者,同時(shí)也是無(wú)前導(dǎo)斜杠標(biāo)準(zhǔn)的信徒,那么我就不需要考慮有人寫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.
這篇關(guān)于在 PHP 中導(dǎo)入類和命名空間:前導(dǎo)反斜杠有什么區(qū)別?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!