1

I have been reading about Using namespaces: Aliasing/Importing in PHP. There are two things I don't understand.

It 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 recommended, as import names must be fully qualified, and are not processed relative to the current namespace.

Can someone please explain

  • What does it mean?
  • What's the purpose of using namespaces aliasing ? Given that I know the purpose of using namespaces.
2
  • Also see What are namespaces and What is a namespace and how is it implemented in PHP. I am not closing yours as dupe of these because you asked for clarification on a particular part of the manual, but I had appreciated if you had somehow mentioned you checked these before (you did, right?). Commented Jul 19, 2013 at 4:44
  • Yeah I did. Thanks for support BTW Commented Jul 19, 2013 at 5:00

2 Answers 2

5

What does it mean?

It really means what it says and shows in the example. When importing a namespaced class, you should omit the first backslash:

use My\Full\Classname as Another;    // recommended
use \My\Full\Classname as Another;   // not recommended

The reason being that use expects a fully qualified namespace. You cannot use a relative path. In other words if you are in the My\ namespace already, you cannot use Full\Classname.

What's the purpose?

It's explained in the first chapter actually:

In the PHP world, namespaces are designed to solve two problems that authors of libraries and applications encounter when creating re-usable code elements such as classes or functions:

  • Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.
  • Ability to alias (or shorten) Extra_Long_Names designed to alleviate the first problem, improving readability of source code.

So, the purpose is to shorten and/or to avoid clashes, e.g. when you have two classes called Foo and need to use both, you have to have a way to resolve that conflict (at least if you don't want to use the fully qualified name each time):

use My\Very\Long\Namespaced\Class\Named\Foo as Foo;
use My\Other\Foo as OtherFoo;

And then you can use

$foo = new Foo;
$otherFoo = new OtherFoo;

So that's short and simple and doesn't clash. There really isn't much more to it.

Sign up to request clarification or add additional context in comments.

Comments

4

You might need to import two totally separate name spaces, that happen to have the same name. Like, maybe you need to select data from mysql and then insert into oracle, and you're using some database library which uses namespacing.

use Database\Mysql\Connection;
use Database\Oracle\Connection;

$conn = new Connection(); //which one is it??

You could either skip importing a namespace

use Database\Mysql\Connection;
use Database\Oracle\Connection;

$conn = new Database\Mysql\Connection();

or alias at least one of them

use Database\Mysql\Connection as MysqlConnection;
use Database\Oracle\Connection as OracleConnection;

$conn = new MysqlConnection();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.