Questions And Answers

More Tutorials

PHP Namespaces

The PHP community has a lot of developers creating lots of code. This means that one library’s PHP code may use the same class name as another library. When both libraries are used in the same namespace, they collide and cause trouble.

Namespaces solve this problem. As described in the PHP reference manual, namespaces may be compared to operating system directories that namespace files; two files with the same name may co-exist in separate directories. Likewise, two PHP classes with the same name may co-exist in separate PHP namespaces.

It is important for you to namespace your code so that it may be used by other developers without fear of colliding with other libraries.

Declaring namespaces

A namespace declaration can look as follows:
  • namespace MyProject; - Declare the namespace MyProject

  • namespace MyProject\Security\Cryptography; - Declare a nested namespace

  • namespace MyProject { ... } - Declare a namespace with enclosing brackets.

  • It is recommended to only declare a single namespace per file, even though you can declare as many as you like in a single file:
    namespace First {
     class A { ... }; // Define class A in the namespace First.
    namespace Second {
     class B { ... }; // Define class B in the namespace Second.
    namespace {
     class C { ... }; // Define class C in the root namespace.

    Every time you declare a namespace, classes you define after that will belong to that namespace:
    namespace MyProject\Shapes;
    class Rectangle { ... }
    class Square { ... }
    class Circle { ... }

    A namespace declaration can be used multiple times in different files. The example above defined three classes in the MyProject\Shapes namespace in a single file. Preferably this would be split up into three files, each starting with namespace MyProject\Shapes;. This is explained in more detail in the PSR-4 standard example.

    Declaring sub-namespaces

    To declare a single namespace with hierarchy use following example:
    namespace MyProject\Sub\Level;
    const CONNECT_OK = 1;
    class Connection { /* ... */ }
    function connect() { /* ... */ }

    The above example creates:
    constant MyProject\Sub\Level\CONNECT_OK
    class MyProject\Sub\Level\Connection and
    function MyProject\Sub\Level\connect


    In this page (written and validated by ) you learned about PHP Namespaces . What's Next? If you are interested in completing PHP tutorial, your next topic will be learning about: PHP Sessions.

    Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

    Share On:

    Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.