Namespaces With PHP

PHP namespaces was introduced in version 5.3. Namespaces is a way of group classes together into virtual directories, the main reason we would want to do this is to help avoid collision of class names in your application.

In PHP you can not define two classes with the same name, if you do this PHP will display a fatal error in your application. Using namespaces means that you can define two classes with the same name as long as they are in different namespaces.

For example if you had two classes both named Table and both classes perform different tasks, one will be used to read a database table and the other will be used to display a HTML table. If these are not in namespaces then PHP will throw a class is already defined error, the old way of fixing this problem would be to prefix the class name with something unique.

The database table class would now be called Database_Class and the HTML class will now be called Html_Class. But instead of doing this you can now use place these classes in different PHP namespaces. This means that now we don't need to extend the name of class and can avoid name collisions of classes and functions.

Defining Namespaces

All your code can be contented by a namespace but only four types of code can be affected by namespaces, classes, interfaces, functions and constants.

To define a new namespace you need to use the namespace keyword and the namespace must be the first thing you declare in your file before any other code.

<?php
namespace NewNamespace;
?>

Now all the code that follows this namespace keyword is grouped inside this namespace and will be able to get to by using this namespace name.

Defining Sub Namespaces

With namespaces just like directories and folders you can use sub namespaces, to define a hierarchy of namespaces.

<?php
namespace NewNamespace\Sub\Level;
?>

Defining Multiple Namespaces

If you want to define a namespace in PHP it must be the first thing that define in your PHP code, but you can define other namespaces in the same file. Though it's not best practice to define two classes in the same PHP file if you do you can define these in different namespaces.

<?php
namespace NewNamespace\Blog;
// following is part of the NewNamespace\Blog namespace
class Article
{

}

namespace NewNamespace\Store;
// following definitions are now part of NewNamespace\Store
class Article
{

}

Using Namespaces

There are 3 ways you can use namespaces in PHP:

  • Unqualified Namespace - used to refer to a file in the current namespace.
  • Qualified Namespace - used to refer to a sub namespace of the current namespace.
  • Full Qualified Namespace - used to refer to any file in any namespace,
namespace ExampleNamespace;

/* Unqualified name */
$foo = new foo(); // resolves to class ExampleNamespace\foo

/* Qualified name */
$foo = new subnamespace\foo(); // resolves to class ExampleNamespace\subnamespace\foo
                                  
/* Fully qualified name */
$foo = new ExampleNamespace\foo(); // resolves to class ExampleNamespace\foo

Use Keyword Namespace

When you have a large application which has many levels of nested namespaces, when you refer to your classes you might have to go multiple levels deep of namespaces like namespace\subnamespace\subsubnamespace\subsubsubnamespace\subsubsubsubnamespace. This can become quite annoying when you have to write all these levels of namespaces on each of the classes. The solution to this is to use the PHP keyword use at the top of your file to import your namespaces. This will allow you to refer to classes by their class name instead of writing the full namespace.

<?php
use subnamespace\subsubnamespace\subsubsubnamespace\subsubsubsubnamespace\Class;

// this refers to subnamespace\subsubnamespace\subsubsubnamespace\subsubsubsubnamespace\Class
$class = new Class()
?>

Conclusion

Namespaces are used to avoid conflicting class and function definitions, this allows you to have shorter, more readable class names. While you can use namespacing you are not obligated to use namespaces but hopefully you will see the organisational benefits of using namespaces in your large applications.

Advertise here 50% Off

Comment