Back to Top

How to use Namespace in PHP

How to use namespace in PHP?

Before some days, I don’t have idea about Namespaces in PHP and I have searched a lot for this and I didn’t understand it and finally lots of headaches, I am clear with namespace now and I want to share my knowledge with you because if you are using PHP 5.3, you have to know about Namespaces and Exceptions.

Namespaces are the core part of ROR(Ruby on Rails). Its new feature in PHP and have released before some years around June 2009 in PHP a. We can say Namespaces are one of the most significant features in PHP 5.3.0 and if we try to use if in older version of PHP, error message would be appear.

Namespaces are the common concept of OOP because it’s a simply collection of classes, functions and objects. We can put any of the PHP code in the namespace, but the main purpose of a namespace is to avoid naming collision of classes, functions and variables.

What are namespaces?

Namespaces are the way to encapsulate classes, functions or constants to a specific library or area to solve certain conflicts that can arise when two different classes are given the same name.With the use of namespaces, we can avoid the naming conflict between one or more classes.when you add code under namespaces, it would be unique from other’s code even if they have the same entity name.

Where can You Use Namespaces?

You probably don’t need namespaces for small applications or applications with only one developer, or applications without frameworks of third party code but if your application have large set of classes libraries and functions within class and If your application has more than one developer, or you start to use third-party libraries, it’s good to use namespaces to keep your code clean, and prevent name conflicts in your code, that are difficult to find and fix.


namespace {ClassName, FunctionName};

To declare a namespace, keyword ‘namespace’ is used and to use the namespace, Scope Resolution Operator (::) or keyword USED is used.

We should declare the namespace as above and we can also create nested namespaces using a backslash() between multiple names of the namespace.

How Do You Use Namespaces?

First Create one demo.php file and add below code:

namespace test;

class Testclass {
 function Welcome() {
   echo 'Welcome'; 

And next create Test.php file which can contain below code:

include "demo.php";
$test = new testTestclass();
// Prints 'Welcome' 

NOTES: Namespace must be declare on top of the page.

Above Example show you the class referenced with the Namespace and after that you can use the Scope Resolution Operator and instantiate the “Test” class.

You can use more than one namespaces in single file like

namespace first;

class FirstClass(){
  // Code
namespace second;

class SecondClass(){
// Code

How to define Nested namespaces in the same file?

namespace MyFirstNamespacesubnamespace;

Alright, we have declared namespace but how to use them its the main thing of it so let’s see how to use nested namespaces.

Also Checkout other posts:

Static Keyword in PHP
Constructor and Destructor in PHP
Type Hinting in PHP5

If we want to use namespace in other files, we can use it with “USE” keyword. so USE is most and important thing for namespaces.


use MyFirstNamespace;

Now with the use of “USE” keyword any constant, classes, or functions in the file will now be part of “MyFirstNamespace”.

You can also view presentation from slideshare:

Comments (8)

  1. I think the whole 5.3 Namespace is horrible. Proper directory structure and an autoloader works well.

    1. Keep it coming, wrietrs, this is good stuff.

      1. Thank you for this post! I am a nwebie and got so lost on my professor’s explanation of modules and name space (more like a terminology burp). You explained it really well.

  2. […] namespaces | Creative Dev […]

    1. Mighty useufl. Make no mistake, I appreciate it.

  3. When you think about it, that’s got to be the right aneswr.

    1. Nice alctire. Very good, explicit and funny examples. One thing I feel the need to mention: using include can sometimes require… require. If the module is in a separate file.

  4. Now I’m like, well duh! Truly thnakful for your help.

Leave a Reply

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

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

Most Popular Posts

How to display ADMIN Toolbar in UI

Posted on 7 years ago


Overview of Amazon Web Services (AWS)

Posted on 2 years ago


Ajax Synchronous and asynchronous requests

Posted on 2 years ago


How to Install WAMP Server on Windows 8

Posted on 6 years ago


Hello world

Updated 5 days ago

Nick Carter

In depth Understanding of Laravel Core

Updated 2 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 4 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 5 years ago