PHP | Namespace

Like C++, PHP Namespaces are the way of encapsulating items so that same names can be reused without name conflicts.

  • It can be seen as an abstract concept in many places. It allows redeclaring the same functions/classes/interfaces/constant functions in the separate namespace without getting the fatal error.
  • A namespace is a hierarchically labeled code block holding a regular PHP code.
  • A namespace can contain valid PHP code.
  • Namespace affects following types of code: classes (including abstracts and traits), interfaces, functions, and constants.
  • Namespaces are declared using the namespace keyword.

A namespace must be declared the namespace at the top of the file before any other code – with one exception: the declare keyword.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
namespace MyNamspaceName {
   
    // Regular PHP code 
    function hello() 
    {
        echo 'Hello I am Running from a namespace!';
    }
}
?>

chevron_right


If namespace is declared globally, then declare it without any name.



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
namespace {
   
    // Global space!
}
?>

chevron_right


Multiple namespaces can be declared within a single PHP code.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
namespace MyNamespace1 {
   
}
    
namespace MyNamespace2 {
   
}
    
namespace {
   
}
?>

chevron_right


A namespace is used to avoid conflicting definitions and introduce more flexibility and organization in the code base. Just like directories, namespace can contain a hierarchy know as subnamespaces. PHP uses the backslash as its namespace separator.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
namespace MyNamespaceName;
function hello() 
    {
        echo 'Hello I am Running from a namespace!';
    }
    
// Resolves to MyNamespaceName\hello
hello();
  
// Explicitly resolves to MyNamespaceName\hello
namespace\hello();
?>

chevron_right


Aliasing in Namespaces

Importing is achieved by using the ‘use’ keyword. Optionally, It can specify a custom alias with the ‘as’ keyword.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
namespace MyNamespaceName;
    
require 'project/database/connection.php';
    
use Project\Database\Connection as Connection;
    
$connection = new Connection();
    
use Project\Database as Database;
    
$connection = new Database\Connection();
?>

chevron_right


It is possible to dynamically call namespaced code, dynamic importing is not supported.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
namespace OtherProject;
    
$title = 'geeks';
    
// This is valid PHP
require 'project/blog/title/' . $title . '.php';
    
// This is not
use Project\Blog\title\$title;
?>

chevron_right


Reference : http://php.net/manual/en/language.namespaces.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.