开发者

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.

But it is said that \My\Namespace is better, because non ambiguous, whereas My\Namespace could be resolved as \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"

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. However, unlike absolute paths, I agree with the less ambiguous \My\Namespace because it will never break, where relative namespaces could.

Namespace aliases, via use add to the abiguity of relative namespace names. For example, I can say: use \PDO as YourMom and later call YourMom within the code. Is it \PDO, \YourMOM, or YourMom that it is being called? Obviously the alias wins and is resolved to \PDO, assuming there is no conflict, but it makes code hard to follow.

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. Thus, namespace My and namespace \My (or swap any other namespace declaration below) are 100% identical as and declaration fully qualifies a namespace.

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.

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.


Actually there is a difference, but not always (ah right).

In the use construct, you never have to 'mention' the leading \. Inline you never have to if the class is in the same namespace, or if you're using an import (imported with 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:

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
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';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜