Namespaces in PHP

After long waiting -  Namespaces  are introduced in PHP.

Let we start with Why we need Namespaces  ?

Just think A child got admission in new school, He made 2 new friends in 1st week let name 1st as ‘chirag’ and 2nd as ‘jatin’, He use to tell his parents story about his friends i.e chirag do this, jatin do that bla bla bla. After a month there is huge difference in his friend circle, now no. of friends in his circle increased from 2 to 20, And among those 20 there are 5 ‘chirag’, Now what …How he will clear his parents  that the story he is saying is about which ‘chirag’ ?  Smart kid he started using last name.

I used above example to illustrates  the concept of namespaces. A namespace is named container used for identifiers like class name and function name used in programs. Namespaces group them together and remove the possibility of name clashes.  In the above example last name of student served as namespaces.

Large applications often have hundreds or thousands of components, which result  in  large number of name clashes. Namespaces allow you to group components and avoid this kind of  problem.

Before namespaces were available, We normally use to prefix each components class with component name such as a class register in user component named as user_register Some time this result into very long naming convention. This practice result into long  and sometimes confusing class names.

How we can define Namespace

code in Namespace is defined using a single namespace keyword at the top  PHP file. It must be the first command (with the exception of declare) and no non-PHP code, HTML, or white-space can prefix the command.

The code following this line will be assigned to the ‘MyFirstNamespace’ namespace. It is not possible to nest namespaces or define more than one namespace for the same code block (only the last will be recognized). You can define different  code for namespace in the same file, e.g.

namespace MyFirstNamespace;
// PHP code for the MyFirstNamespacenamespace
namespace MyFirstNamespace2;
// PHP code for the MyFirstNamespace2namespace
// Alternative syntax
namespace MyFirstNamespace3{
	// PHP code for the MyFirstNamespace3

Its possible, Even though I would advice against it, Use single namespace per file.

Calling Namespace Code

In a file named namespacelib1, we will define a constant, a function, and a class within the App\NamespaceLib1  namespace:


namespace App\NamespaceLib1;
const CONST1 = 'App\NamespaceLib1\CONST1';
function Function1() {
	return __FUNCTION__;
class Class1 {
	static function FirstFunction() {
		return __METHOD__;

We can now include this code in another PHP file, e.g.


header('Content-type: text/plain');
echo \App\NamespaceLib1 \CONST1 . "\n";
echo \App\NamespaceLib1 \Function1() . "\n";
echo \App\NamespaceLib1 \Class1::FirstFunction() . "\n";

No namespace is defined in NamespaceLib2.php so the code exists in the global space. Any direct reference to CONST1 , Function1 or Class1will fail because they exist in the App\NamespaceLib1 namespace. To call code in NamespaceLib1.php, we can add a prefix of \App\NamespaceLib1 to define fully-qualified names. The following result is output when we load namespacelib2.php:


Fully-qualified names can become quite long and there are few obvious benefits over defining long class names such as App-NamespaceLib1-Class1.

In next article we will discuss how Exactly Namespace work in PHP.