PHP namespaces: \ My \ Namespace or My \ Namespace?

  • My\Namespace
    • \My\Namespace

So which one should I use, I see that the php documentation uses mainly My\Namespace .

But it is said that \My\Namespace better because it is not ambiguous, while My\Namespace can be resolved as \RandomNamespace\My\Namespace .

I started thinking about this after reading Doctrine 2 about this problem : Please replace the "Doctrine \ XXX \ YYY 'with' \ Doctrine \ XXX \ YYY 'in the code and document "

So, do you have more information about this? Thanks

+7
source share
3 answers

This is nothing more than using relative vs files with an absolute / uri file, and this is reduced to preference. However, unlike absolute paths, I agree with the less ambiguous \My\Namespace , because it never breaks where relative namespaces may refer.

Alias ​​names, through use add to the inconstancy of relative namespace names. For example, I can say: use \PDO as YourMom and later call YourMom inside the code. Is it \PDO , \YourMOM or YourMom , what is it called? Obviously, the alias wins and resolves \PDO , assuming that there is no conflict, but it makes code execution very difficult.

Edit

Sample code to prove irreparable is possible. Make sure you use the global fallback if the namespace is unqualified.

As @netcoder has already mentioned, it is not possible to be inconsistent during a namespace declaration . Thus, namespace My and namespace \My (or swapping any other namespace declaration below) are 100% identical, and the declaration fully defines the 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 } 
+4
source

In the namespace declaration itself, this does not matter. This declaration always defines the full name, so you can safely omit the leading namespace delimiter. This is 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 delimiter guarantees that this determinant is absolute, if absent, its relative. See the manual on how they are resolved.

If you use only classes from the same namespace and / or β€œdeclare” each class at the top of the file through use (and possibly as ), you can safely use relative class identifiers.

+3
source

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

In the use construct, you do not need to "specify" the leading \ . Inline you never have to if the class is in the same namespace, or if you are using import (imported using use ns ).

But sometimes you have to:

 namespace foo; class bar extends \baz\Bar { 

You are using the built-in class undefined / unknown / unimported, so you must specify its source.

Another example is classes with names that are not used in the namespace, inline:

 namespace foo; $dt = new \DateTime; 

The best practice (as a rule) is to import all the classes required by the current file. The usage 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 tricky function __construct() { $dt = new DateTime; 

change 1
Also, be sure to use class name names with name names when passing them as strings, even if you are in the correct namespace:

 namespace foo; $class = 'foo\bar'; 
+2
source

All Articles