Sharing technology, ideas, insights!
Call: +91 710 466 0336         Email:


Namespaces in PHP 5.3 – Part 2

Welcome back! In Part 1 of this post we discussed how to create namespaces and use them for classes, functions and constants. If you have not read Part 1 I strongly recommend you read it before we move on to some of the advanced parts related to namespaces. Lets start from where we had left.

Ok great, Is that all I need to know about namespaces ?

While you know now the important parts we are not done yet. There are quite a few important points that really help use namespaces to the fullest. This includes aliasing / importing etc. Without delay lets just jump to them.

What is aliasing. How to create and use alias ?

Consider this code for ‘Log.php’ :
Say this is a class for writing and reading Logs inside quite a looongg namespace.

Above is just an example class created to explain the use of namespaces,
Now, say you want to use this. What you would do to use the above class is :

lets call it ‘useLog.php’

If you had to write it once, you would probably forgive me. But say I tell you that you need multiple calls what would you do ? Even worse, say I tell you to instantiate multiple such classes with different namespaces ? Scary isn’t it ?

Enter ‘alias’ :
Well aliases are similar to the ability of unix-based filesystems to create symbolic links to a file or to a directory, in our case this can be an alias to a namespace or a class in a namespace. See how we re-wrote the ‘useLog.php’ to make use of aliases using the ‘use’ keyword.

So, you can see we created a kind of link or alias here to get rid of the huge anaconda namespace.

Again, note that the following code snippets 1 and 2 work same.

Snippet 1 :

Snippet 2 :

That is if you don’t explicitly want to declare or alias using the ‘as’ keyword. PHP assumes that you want to set alias ‘Web’ i.e the last part.

So this is called namespace aliasing / importing. Importing ? When did we do that ? Well in code Snippets what we did there was that we used the ‘use’ keyword and ‘imported’ that namespace.

Ok great, But how do I use classes, functions and constants now ?

Neither functions nor constants can be imported via the use statement. However, You can use the above ‘use’ keyword to import classes the same way. Consider another version of ‘useLog.php’

You can use the the alias to use a class, or the namespaced alias to call a function or use a constant like below :

How do I know which is the current namespace ?

PHP provides the __NAMESPACE__ magic constant just for this and you can use it to get the value of the current namespace. In global, un-namespaced code, it contains an empty string. Now, while you have this magic constant, there also another way to reference the current namespace. The namespace keyword can be used to explicitly request an element from the current namespace or a sub-namespace. It basically does the same thing for namespaces as the the self operator for classes.

Consider the following code that uses both of the above ways:

What the above code does is that it declares a namespace, then echos the current one, we then use our earlier namespaced code and call its debug function. Finally we declare a debug function in the current namespace and then refer to the current namespace using the ‘namespace’ keyword.

What about auto loading ? When I use that what happens ?

In PHP 5.3 when you use autoloading with namespaces the complete fully-qualified namespace and class name get passed to the autoload function.

Consider this simple example.
Lets say you have all your classes tucked away in different folders in a hierarchy similar to the namespace itself:

As you can see in the above code you can use the namespaces passed to auto load the files, while this is just one example you can use it based on your applications needs.

There are however some name resolution rules that should be carefully read so that there are no “Oh! Why is it not working for me?” kind of situations. The PHP manual describes these rules in details.

By now you are now familiar with one of the latest and best features PHP has to offer. In fact many PHP frameworks like Zend Framework and Symphony have already started to leverage this for their upcoming versions and others are on the way. So when are you going to start using them ?

As usual I am waiting for your views, comments and suggestions. And I’ll be back with another interesting post. Until next time. Happy Namespacing!

About the Author

Jatin has been working with PHP for the last 7 years and is a Zend Certified PHP 5 Engineer.

One comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.