• PHP Namespace


    In 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.

    Silver Member Asked on September 13, 2016 in Php.
    Add Comment
  • 1 Answer(s)
    Best answer

    PHP Namespace

    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 with 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 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 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 of 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 constructor to initialize class properties, while creating objects, and we have a function under this name space to print initialized value.

    Since, it is an 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 classe 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 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 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.

    Silver Member Answered on September 13, 2016.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.