Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing classes and namespaces in PHP: What difference does a leading backslash make?

What's the difference between those two:

use Exception;
use \Exception;

Or those:

use Foo\Bar;
use \Foo\Bar;

The manual says:

Note that for namespaced names (fully qualified namespace names containing namespace separator, such as Foo\Bar 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.

But I don't really understand this, as all of the above variants work, i.e. it definitely is not "not allowed".

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;
}

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.)

So: Am I missing something? Is there actually some difference?

like image 634
NikiC Avatar asked Jan 31 '11 09:01

NikiC


People also ask

What does backslash mean in PHP?

\ (backslash) is the namespace separator in PHP 5.3. A \ before the beginning of a function represents the Global Namespace. Putting it there will ensure that the function called is from the global namespace, even if there is a function by the same name in the current namespace.

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.

What is a namespace in PHP?

Namespaces are qualifiers that solve two different problems: They allow for better organization by grouping classes that work together to perform a task. They allow the same name to be used for more than one class.

How can use namespace in another file in PHP?

A file containing a namespace must declare the namespace at the top of the file before any other code - with one exception: the declare keyword. Add this to your included file as well. namespace my_ns; After that, your code works just fine.


3 Answers

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.

However, there is something else troubling with the manual. They advertise this:

// importing a global class
use \ArrayObject;

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.

Also, with bugs such as this: http://bugs.php.net/bug.php?id=49143

I believe there is confusion over what the standard is supposed to be.

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.

like image 54
erisco Avatar answered Oct 19 '22 19:10

erisco


In fact there is no difference in using leading backslash in importing namespaces at the moment and also information in PHP manual has changed:

Note that for namespaced names (fully qualified namespace names containing namespace separator, such as Foo\Bar as opposed to global names that do not, such as FooBar), the leading backslash is unnecessary and not recommended, as import names must be fully qualified, and are not processed relative to the current namespace.

So now there is true information that using leading backslash is not recommended but there is no info that it's not allowed at it was in past.

So at the moment:

use Exception;
use \Exception;

those 2 lines work the same but you should rather use the first one.

like image 41
Marcin Nabiałek Avatar answered Oct 19 '22 20:10

Marcin Nabiałek


Usually the leading backslash defines, that the identifier is absolute. If its missing, the interpreter assumes, that it is a relative identifier.

This is an absolute identifier:

$x = new \Name\Space\To\Class();

This is a relative identifier, because of the no leading slash. It's relative to the current namespace:

namespace Name\Space;
$x = new To\Class;

This is also a relative identifier. In this case, its resolved against the use statement, because the last part (alias) is the same, as the first of the class:

namespace Other\Name\Space;
use Name\Space;
$x = new Space\To\Class;

However, because in namespace and use statements only absolute identifiers (fully qualified names) are allowed, it's ok to omit it here. In namespace, it's even not allowed to set the leading backslash.

For further information on how PHP resolves the different namespace declarations, see the namespace rules manual.

like image 6
KingCrunch Avatar answered Oct 19 '22 20:10

KingCrunch