PHP

Using namespaces in PHP 5 and PHP 7

The biggest new feature in PHP 5.3 is support for namespaces. This feature lets you avoid naming clashes across different parts of an application, or between application libraries. Ordinarily, you can not use the same function name, class name, or constant name for different purposes within the same application. But sometimes this can happen, for example, if you’re working with a large application, or including many libraries.

Namespaces help to avoid these naming collisions between libraries or other shared code. A namespace will encapsulate the codes (classes, functions and constants const) inside it so that they don’t conflict with items declared elsewhere.

Creating a namespace

To create a namespace in PHP, use the namespace keyword followed by the namespace name. The namespace declaration must be the very first line after opening <?php tag:

<?php
namespace brainbell;

 aFunction() {
  return true;
 }
 
 bFunction(){
  return false;
 }

Note: The file containing namespaced code must declare the namespace at the top of the file before any other code or HTML.

Once the namespace declared at the top of a PHP file, all code within that file belongs to that namespace. In above example, the aFunciton and bFunction functions belongs to the brainbell.

It is also possible to have two or more namespaces in a file by wrapping the code for the namespace in braces:

<?php

namespace first {
  // ...
 }

namespace second {
  // ...
 }

namespace {
  // the global namespace
 }

Note: This usage is not standard practice. Although you can declare multiple namespaces within a single PHP file, this is not good coding practice.

Global namespace

A file that not declared namespace keyword at the top; belongs to the global namespace. For example:

//Global namespace
<?php
 function aFunction(){
  return true;
 }

namespace example

Once you’ve declared a namespace at the top of a PHP file, all code within that file belongs to that namespace. To use a class, function or constant that was declared in the same namespace, just write its name as normal, for example: $var = aFunction();:

<?php
namespace brainbell;
 //a.php

 function aFunction() {
  return true;
 }
 
 $var = aFunction(); //true

To use a namespaced class, function or constant from outside a namespace, write the namespace name, followed by a backslash \, followed by the class, function or constant name, for example:

<?php
 //global namespace
 //b.php

 require ( 'a.php' );

 $var = brainbell\aFunction(); //true

Let’s create the same function name aFunction inside the b.php :

<?php
 //global namespace
 //b.php

 require ( 'a.php' );

 function aFunction() {
  return 'hello world';
 } 
 
 $var1 = aFunction(); // hello world
 $var  = brainbell\aFunction(); // true

The above example will generate the following error if you remove the namespace brainbell; from the top of a.php file:

Fatal error: Cannot redeclare aFunction() (previously declared in b.php:8) in a.php on line 7

Thanks to PHP namespace, the names of both aFunction() functions no longer clash.

Sub-namespaces

You can also create sub-namespaces, much like sub-folders (or sub-directories) in a file system. This allows you to create a namespace hierarchy. You separate each namespace level with a backslash \ character:

<?php
namespace brainbell\template

The above code declares the template namespace inside the brainbell namespace. Let’s declare the same name function aFunction inside the sub-namespaced file and save it as c.php.

<?php
namespace brainbell\template
//c.php

function aFunction(){
 return 100;
}

Next, edit the b.php (which we’ve already created in previous example) and include the c.php file. Now you can see that all files a.php, b.php and c.php declared the aFunction() function but they not clashing with each other nor generating any error because they are all in different namespaces:

<?php
 //b.php

 require ( 'a.php' ); // brainbell
 require ( 'c.php' ); // brainbell\template
 
 function aFunction() {
  return 'hello world';
 }

 $var  = aFunction(); // hello world
 $var1 = brainbell\aFunction(); // true
 $var2 = brainbell\template\aFunction(); //100

 //Print information
 echo '<pre>';
 var_dump($var);
 var_dump($var1);
 var_dump($var2);

Let’s create another file d.php and write the following code to call brainbell\template\aFunction() from code that is within the brainbell namespace:

<?php
namespace brainbell;
//d.php
require ('c.php'); //brainbell\template

$var = template\aFunction(); //100
var_dump($var);

You can specify namespaces using relative notation, much like working with relative paths.

Namespace aliases

If you’re working with a big namespace hierarchy, specifying the full namespace name each time can get tiresome:

<?php
require 'c.php';
$var = brainbell\template\db\views\aFunction();

To get around this problem, you can use a namespace alias , which works much like a symbolic link in UNIX file systems. To create an alias you write:

use namespace as alias;

Once you’ve created an alias, you can use it in place of the full namespace name:

<?php
require 'c.php';
use brainbell\template\db\views as dbviews;
$var = dbviews\aFunction();

You can leave out the as statement (and the name that follows), in which case the use statement uses the last portion of the namespace name as the alias. So the following two lines of code are equivalent:

use brainbell\template\db\views as views;
use brainbell\template\db\views;

Summary

  • Namespaces help you avoid naming collisions between libraries or other shared code.
  • As well as functions, you can also use namespaces with classes and constants (but you must declare them with the const keyword and not with define()).
  • It is possible to have more than one namespaces in a file, but it is not recommended.
  • If you are working in a namespace, then the interpreter will assume that names are relative to the current namespace.
  • You can use the use keyword to import or alias a namespace, class, function or constant:
    <?php
    require 'c.php';
    
    //namespace aliasing
    use brainbell\template as tpl;
    
    //function aliasing, since PHP 5.6
    use function brainbell\template\aFunction as myfnc;
    
    //constant aliasing, since PHP 5.6
    use const name\space\constName as cnst;
    
    

For more information visit: http://php.net/manual/en/language.namespaces.importing.php

Advertisement:
Advertisement: