Namespacing in php

Why namespaces?

When an application grows bigger and code gets more involved, chances are that you are accidentally reusing the same function or class name. As a consequence, an error occurs. If you’re using external libraries, you even have a bigger change of creating an error. Imagine that an external library is using a general database class (e.g. include(‘database’)). For your application you have written your own database class and you include this at the top of your files. Unfortunately, this will throw an error because you are redeclaring a class.

The solution is to use namespacing and this blog covers an introduction to this topic. Without namespacing, the issues described above could only be solved by using long, describing class and function names, or by prefixing them. WordPress, for example, uses the ‘wp_’ in all its function and class names.

By default, all code will be placed in the global space. However, when you apply namespacing, and start your php file with the following:

namespace myapplication;

All code in this file that follows now belongs to the myapplication namespace instead of the global space. Nesting of namespaces is not possible, but you can use multiple namespaces in one file:

namespace myapplication1;
\\code from the first namespace

namespace myapplication2;
\\code from the second namespace

However, using multiple namespaces in one file doesn’t contribute to the clarity of your code, so its better to use a single namespace in each file.

Subnamespaces

A hierarchy may be created by the use of subnamespaces. Just use the backslash to separate the subnamespaces, e.g.

namespace application\part1;

The use of namespaced code

As an example we create a library with several useful functions in a file called helper.php.

namespace application\helper;

class Helper
{
    public static function printHello () 
    {
        echo ‘Hello’;
    }
}

Next, we want to use the helper library in our application, which we have written in a file called application.php. If we now use the code:

require_once(‘helper.php’);
Helper::printHello();

nothing will happen. The code in application.php lives in the global space, while the code in the helper library lives in the application\helper space. To solve this, you do the following:

require_once(‘helper.php’);
application\helper\Helper::printHello();

Of course, this is not a very useful situation. Instead of long function and class names, you now have to use an extended namespace in front of the function call.

Importing namespaces

The code above can be simplified if you import the namespace and start your application.php file with:

use application\helper;

application.php still lives in the global namespace, so we cannot directly call the function from the helper class, but it get’s already simpler:

helper\Helper::printHello();

php will now look for the imported namespace and search for a function printHello();

Aliases

To simplify matters even more, an alias can be attributed to a namespace:

1.) use application\helper as h
2.) use application\helper\Helper as Help

The second method only works for classes, not for functions or constants. The function printHello() can now be called as follows:

1.) h\Helper::printHello();
2.) Help::printHello();

In case of classes, the second option is favorable, because it has the shortest syntax and it remains clear what is going on.

Debugging

With the following command, the current namespace will be displayed.

echo __NAMESPACE__;

Namespaces are supported for php5.3 and higer.

Concluding remarks

With namespaces, it’s easy to keep your code clean and organised, while you avoid a potential clash between different parts of your application. Please leaves your comments below in case you have questions or remarks.


Mijn Twitter profiel Mijn Facebook profiel
Leonie Derendorp Webdeveloper and co-owner of PLint-sites in Sittard, The Netherlands. I love to create complex webapplications using Laravel! All posts
View all posts by Leonie Derendorp

Leave a Reply

Your email address will not be published. Required fields are marked *