Header Ads

What is Namespace in PHP?

One particularly useful new addition to PHP 5.3.0 is namespaces. Namespaces allow developers to sandbox their code, outside of the global namespace, to avoid class name conflicts and help organize their code better.We will make use of namespaces in almost all of the classes we write, and they are not particularly tricky to use. Consider the example presented below:

Example :

namespace Framework
{
  class Hello
    {
      public function world()
      {

       echo "Hello world!";
        }
     }
 }
 

namespace Foo
                    {
           // allows us to refer to the Hello class
         // without specifying its namespace each time use Framework\Hello as Hello;
                      

                     class Bar
                               {
                                function   construct()
                                {
            // here we can refer to Framework\Hello as simply Hello
            // due to the preceding "use" statement
                                     $hello = new Hello();
                                     $hello->world();
                                   }
                                   }
                      }
                namespace
                           {
                          $hello = new Framework\Hello();
                          $hello->world(); //… prints "Hello world!"
                          $foo = new Foo\Bar();
                          $foo->bar(); //… prints "Hello world!"
                            }


As mentioned before, namespaces help to remove classes from the global namespace. The namespace itself
remains within the global namespace, so it must remain unique; however, it can contain any number of classes,
which can reuse class names that are in the global namespace, or within other namespaces.

No comments