Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 951
Matthieu Napoli Avatar asked Apr 08 '11 21:04

Matthieu Napoli


People also ask

Can I use two namespace in PHP?

Defining multiple namespaces in the same file ¶Multiple namespaces may also be declared in the same file. There are two allowed syntaxes. This syntax is not recommended for combining namespaces into a single file. Instead it is recommended to use the alternate bracketed syntax.

What are namespaces for PHP?

A namespace is a hierarchically labeled code block holding a regular PHP code. A namespace can contain valid PHP code. Namespace affects following types of code: classes (including abstracts and traits), interfaces, functions, and constants. Namespaces are declared using the namespace keyword.

Should I use namespace PHP?

The main objective of namespaces is to prevent name collisions, more over they are used to group classes, methods. As you mentioned there are a lot of classes within a Laravel project so namespaces would have to be used to prevent collisions which will happen in big projects.

What is the difference between namespace and use in PHP?

A namespace is a way of grouping identifiers so that they don't clash. Using a class implies that you can create an instance of that class, not true with namespaces. 2. You can use using-declarations with namespaces, and that's not possible with classes unless you derive from them.


1 Answers

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
}
like image 140
Kevin Peno Avatar answered Oct 09 '22 10:10

Kevin Peno