简体   繁体   English

PHP命名空间:\\ My \\ Namespace或My \\ Namespace?

[英]PHP namespaces : \My\Namespace or My\Namespace?

  • My\\Namespace
    • \\My\\Namespace

So, which one should I use, I see the php documentation uses mostly My\\Namespace . 那么,我应该使用哪一个,我看到php文档主要使用My\\Namespace

But it is said that \\My\\Namespace is better, because non ambiguous, whereas My\\Namespace could be resolved as \\RandomNamespace\\My\\Namespace . 但据说\\My\\Namespace更好,因为非模糊,而My\\Namespace可以解析为\\RandomNamespace\\My\\Namespace

I've started to wonder about this after reading there is an issue in Doctrine 2 about this : " Please replace 'Doctrine\\XXX\\YYY' with '\\Doctrine\\XXX\\YYY' in code and document " 在阅读了Doctrine 2中有一个问题后,我开始怀疑这个问题 :“ 请在代码和文档中用'\\ Doctrine \\ XXX \\ YYY'替换'Doctrine \\ XXX \\ YYY'

So, do you have any more information about this ? 那么,你有关于此的更多信息吗? Thanks 谢谢

This is no different than using relative vs absolute file/uri paths and will come down to preference. 这与使用相对vs绝对文件/ uri路径没有什么不同,并且将归结为首选项。 However, unlike absolute paths, I agree with the less ambiguous \\My\\Namespace because it will never break, where relative namespaces could. 但是,与绝对路径不同,我同意不太模糊的\\My\\Namespace因为它永远不会破坏,相对命名空间可以。

Namespace aliases, via use add to the abiguity of relative namespace names. 命名空间别名,通过use添加相对命名空间名称的含义。 For example, I can say: use \\PDO as YourMom and later call YourMom within the code. 例如,我可以说: use \\PDO as YourMom ,然后在代码中调用YourMom Is it \\PDO , \\YourMOM , or YourMom that it is being called? 它被称为\\PDO\\YourMOMYourMom吗? Obviously the alias wins and is resolved to \\PDO , assuming there is no conflict, but it makes code hard to follow. 显然别名胜出并解决为\\PDO ,假设没有冲突,但它使代码很难遵循。

Edit 编辑

Code example to prove abiguity is possible. 可以用来证明歧义的代码示例。 Make sure you see the global fallback applied if a namespace is unqualified. 如果命名空间不合格,请确保您看到应用了全局回退

As mentioned by @netcoder, it is impossible to be abiguous during the declaration of a namespace. 正如@netcoder所提到的,在声明命名空间期间是不可能的。 Thus, namespace My and namespace \\My (or swap any other namespace declaration below) are 100% identical as and declaration fully qualifies a namespace. 因此, namespace Mynamespace \\My (或交换下面的任何其他名称空间声明)100%完全相同,并且声明完全限定名称空间。

namespace My
{
}

namespace My\Other
{
    use Bob; // Resolves to \My\Other\Bob
}

namespace My\Other\NS
{
    use \My\Other as Other;
    use Other\NS as Duh; // This resolves to \My\Other\NS
}

namespace My\Other\Bob
{
    use Other\NS as Duh; // This resolves to \Other\NS becuase the namespace doesn't exist inside \My\Other\Bob
}

In the namespace declaration itself it makes no difference. 在命名空间声明本身,它没有任何区别。 This declaration defines always a full qualified name, so you can safely omit the leading namespace separator. 此声明始终定义完整限定名称,因此您可以安全地省略前导命名空间分隔符。 This is also true for the use statement. 对于use语句也是如此。

namespace My\Namespace;
// Is exactly the same as 
namespace \My\Namespace;

use Foo\Bar as Class1;
// Is exactly the same as 
use \Foo\Bar as Class2;

In all other cases the leading separator ensures, that the given qualifier is absolut, if its missing, its relative. 在所有其他情况下,前导分隔符确保给定的限定符是绝对值,如果它缺失,则为相对值。 See the manual on how they are resolved. 请参阅手册 ,了解它们的解决方法。

If you use only classes from the same namespace and/or "declare" every class at top of the file via use (and maybe as ), you can safely use relative class identifiers. 如果您只使用来自同一名称空间的类和/或通过use (也许as )“声明”文件顶部的每个类,您可以安全地使用相对类标识符。

Actually there is a difference, but not always (ah right). 实际上有区别,但并不总是(啊对)。

In the use construct, you never have to 'mention' the leading \\ . use构造中,您永远不必“提及”前导\\ Inline you never have to if the class is in the same namespace, or if you're using an import (imported with use ns ). 如果类在同一名称空间中,或者如果您正在使用导入( use ns导入),则内联您永远不会。

But sometimes you must: 但有时你必须:

namespace foo;

class bar extends \baz\Bar {

You're using an undefined/unknown/unimported class inline, so you have to mention its source. 您正在使用未定义/未知/未排序的内联类,因此您必须提及其来源。

Another example is with unnamespaced classes used in a namespace, inline: 另一个例子是命名空间中使用的unnamespaced类,内联:

namespace foo;

$dt = new \DateTime;

A best practice (generally) is to import ALL classes the current file needs. 最佳实践(通常)是导入当前文件所需的所有类。 A use statement is very, very, very, very cheap, so don't hold back. 使用声明非常,非常,非常便宜,所以不要拖延。

namespace foo;

use baz\Bar AS OtherBar;
use \DateTime;

class Bar extends OtherBar { // or something like that; in this case (same class name) it's tricky
  function __construct() {
    $dt = new DateTime;

edit 1 编辑1
Also, don't forget to use fully namespaced class names when passing them as strings, even though you might be in the right namespace: 此外,即使您可能位于正确的命名空间中,也不要忘记在将它们作为字符串传递时使用完全命名空间的类名:

namespace foo;

$class = 'foo\bar';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM