Dec 31, 2012
Table of contents:
When you progress from creating simple websites and web applications, to more complex and bigger projects, you will probably come across the term Namespacing. Namespacing is an important concept in computer programming as it enables you to create modular and extensive applications. Namespacing is a language agnostic term that forms one of the inherent features of good programming languages.
If you’ve been dabbling in learning to code for a while, and you’ve only just come across Namespacing, you might thinking, “Why do I need Namespacing, I’ve gotten this far without it?”. Whilst Namespacing is not critical to writing code that works, it will be critical once your project reaches a certain scale. It is also incredibly important once you start integrating third party libraries into your work.
For this tutorial I will be giving examples of Namespacing in PHP. PHP added Namespacing in version 5.3, so if you are following along, make sure you are running an up-to-date version.
As I mentioned in What is MVC?, programming concepts can seem completely abstract unless they are described within a real life environment. Here is an analogy that describes how Namespacing actually works.
Imagine a class of school children. There are 30 kids in the class and so keeping track of everyone can be a little bit difficult. To make matters worse, some of the parents of the children weren’t very imaginative when they named their kids. In this particular class, there are 3 Johns, 2 Stephens, a set of twins called May and Robert, and two other kids called Mary and Robert who aren’t related at all. In order to distinguish the children, we can use their second name to “Namespace” them.
For example: Jones\John Cooper\John Lilly\John West\Stephen Johnson\Stephen Foster\Mary Foster\Robert Smith\Mary Yellow\Robert
Now we can see exactly where each of the children “originates”.
Taking this example further, imagine the class goes on a trip to a theme park. Each of the children are given a ticket Id number in order to get in to the park. There is also many different schools attending the theme park on the same day.
By Namespacing the class, it doesn’t matter if one of the children has the same Id number as a child from a different school because we can distinguish between the children like this:
St Cuthberts\Foster\Mary\567 Hilly Valley\Smith\Harry\567
As you can see in both of these examples, Namespacing has a number of advantages when things get more complex. In the first example, if we knew that all of the kids in the class had different names, we wouldn’t need to Namespace. In the second example, Namespacing allows us to have children with the same name, or reuse Id numbers without having to worry about the children getting mixed up with kids from other schools.
Namespacing is important because it would be impossible to use two different libraries of code together because there would inevitably be name clashes. When two method names clash, it would result in a undesirable outcome because the wrong method might be called.
Ok, so now you should have an understanding of what is Namespacing, and why you would want to use, let’s jump into some code to see it in action.
Imagine we have an Animal Class:
class Animal
{
// Methods
}
We could simply create a new instance of the Animal Class like this:
// Include the Class
include "Animal.php";
// Instantiate new Animal
$animal = new Animal();
When the project is small, this is perfectly fine. But what if we want to use a library that also has a Class called Animal?
To add a Namespace to the Class, we simply use the keyword “Namespace” and give it a name. The Namespace should be the first thing that is declared in the file, and you should keep one Namespace per file (in the same way you should keep one Class per file).
For example:
namespace ZooLibrary\Evolution;
class Animal
{
// Methods
}
Then we could use this Class like this:
// Include the Class
include "ZooLibrary/evolution/Animal.php";
// Instantiate a new Animal
$animal = new \ZooLibrary\Evolution\Animal();
There’s a few things to note here. Firstly, notice how the Namespace and the directory path to the Class file are the same? This is an important convention to stick to. Secondly, when you want to instantiate a new Animal, you must use the fully qualified name so PHP knows which exact Class you mean.
Importing and Aliasing are two further important terms when learning about Namespacing. Importing allows you to import a Namespace which means you don’t have to write out the full qualified name.
So we could write the previous example like this:
// Include the Class
include "ZooLibrary/evolution/Animal.php";
// Import the Animal Class
use ZooLibrary\Evolution\Animal;
// We can now instantiate like this
$animal = new Animal();
You can also use an Alias which means you can give it a different name:
// Include the Class
include "ZooLibrary/evolution/Animal.php";
// Import that Animal Class
use ZooLibrary\Evolution\Animal as Ani;
// We can now instantiate like this
$animal = new Ani();
It’s probably still hard to appreciate the importance of Namespacing until you find yourself in a situation when you need it. The ah ha! moment will be when you are trying to instantiate a Class, but the wrong Class is getting called.
Namespacing is an important term in all programming languages. Although PHP has only recently added Namespacing support, it will be a huge part of the future development of the language.
Namespacing allows you as a Developer to integrate tried and tested libraries into your code. In the past, it was often the case that you either had to use a framework, or roll your own. If the framework didn’t do what you needed to, you would have to rip it out or completely switch frameworks. Now it is possible to “roll your own framework” by simply picking and choosing which elements you want from each of the various frameworks.
If you are solving a particular problem, and you think it could be useful to other Developers, you can also make your library available to others. By correctly Namespacing it, you will make it very easy for other Developers to integrate your code into their projects.