PHP Namespace

by Vincy. Last modified on July 9th, 2022.

In the file system, we have directories or folders to keep related files. Similarly, PHP namespaces are used to have related code blocks of a PHP program that are reusable, like, classes, functions, interfaces under its reference.

And, PHP namespaces can also capable of containing constants.

Operations Performed using PHP NameSpaces

With PHP namespaces, we can have the following operations listed below.

  • Grouping related PHP code blocks into one scope or space to refer in future.
  • Creating functions or classes with the same name that already exist in user space or internal code structure without name clashes.
  • Getting current namespace with appropriate PHP keyword or constants.
  • Importing external classes, interfaces, and namespaces with an alias.
    • Aliasing will avoid name clashes and for shortening the name of imported blocks for easy readability.
    • Aliasing or importing are not applicable for importing external functions.
  • Accessing classes, functions and etc., from global space.

Declaring PHP Namespace

PHP namespaces are declared by namespace keyword followed by name of the namespace. And, importantly, this namespace declaration should be a very first line before writing any code to our PHP script. But, there is one exception, that is, it allows PHP declare statement before namespace declaration.

Following PHP code shows how to declare namespace contains PHP classes, functions, and constants.

namespace Database;

class DBController

    public $database_name;

    function __construct()
        $this->database_name = "db_namespace";

function print_database_name($objDBController)
    echo $objDBController->database_name;

So, we have created a namespace named as Database, containing classes and functions to handle database related operations. Simply, we have a class with a constructor to initialize class properties, while creating objects, and we have a function under this namespace to print initialized value.

Since it is a namespaced class and function with the name of the class will not be treated as a constructor, we have used __construct() magic function. Otherwise, no initialization will be done for any object.

Accessing Namespaced Classes and Functions in PHP

There are various ways in accessing namespaced classes, functions and etc. These are listed below.

  1. Normal Access
  2. Accessing from global space
  3. Accessing with namespace keyword
  4. Accessing with __NAMESPACE__ keyword

Normal Access

This kind of access is very familiar as we have practiced on creating objects to access class members and invoking local functions. For example,

$objDBController = new DBController();

In the first line of above PHP code, we have created an object for the namespaces classes DBController(). Then, the PHP will search for Database\DBController, meaning that, it is searching for the class there exist under current namespace. So, we need not specify namespace’s name, like,

$objDBController = new Database\DBController();

Then, PHP will search for Database\Database\DBController and display class not found error, like,

Fatal error: Class 'Database\Database\DBController' not found...

Accessing from Global Space

For accessing from global space out of this namespace scope, we should add backslashes (\) before classes, functions or some name, what we want to access from global space. For example,

$objDBController = new \Database\DBController();

The above code will search for the namespaces Database and then specified classes and function under this namespace. So, the above code will create the same effect as that of normal access, without any class not found errors.

Accessing with namespace Keyword

PHP namespace keyword is used to get the current namespace. So, we can specify required members of the current namespace to be associated with this keyword. And the code is,

$objDBController = new namespace\DBController();

Accessing with namespace Keyword

This is similar to PHP namespace keyword for getting current namespace. For example,

__NAMESPACE__ . print_database_name($objDBController);

We should not add backslashes following this constant. Instead, we need to separate it by using PHP dot(.) Operator.

Hope this article will help to gain basic knowledge about PHP namespaces. But, This is only an introductory article. We will see more in future.

Download PHP Namespace Source Code

Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Leave a Reply

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

↑ Back to Top

Share this page