Organize Code

Namespace

PHP

 

Defining Namespace

PHP 5
<?php
namespace myproject;
  
function greet(){
    print('Hi!');
}

Namespaces are used to avoid conflicting definitions and introduce more flexibility and organization in the codebase.

Only classes (including abstracts and traits), interfaces, functions and constants are affected by namespaces.

A file containing a namespace must declare the namespace at the top of the file before any other code.

You can have multiple namespaces in one file, or have a namespace defined in multiple files.

Magic constant __NAMESPACE__ is always a string containing the current namespace name.

 

Namespace Hierarchy

PHP 5
// myproject/Greeting.php file
<?php
namespace myproject;

function greet(){
    print('Hi!');
}

// myproject/english/Greeting.php file
<?php
namespace myproject\english;

function greet(){
    print('Hello!');
}

// myproject/spanish/Greeting.php file
<?php
namespace myproject\spanish;

function greet(){
    print('Hola!');
}

PHP namespaces can have hierarchical structure, much like directories and files in the file system.

The convension is to have nested directories with a same names for each Namespace reflecting the Namespace hierarchy.

 

Bracket Namespace

PHP 5
// Greetings.php file
<?php

namespace myproject\english {
  function greet(){
      print('Hello!'); 
  }
}

namespace myproject\spanish {
  function greet(){
      print('Hola!');
  }
}

namespace {
  function greet(){
      print('Global Hi!'); 
  }
}

You can have multiple namespaces in one file. In that case it is recommended to define Bracket namespaces ({}).

Bracket namespaces and unbracket namespaces cannot be mixed in one file.

In PHP, it is recommended that each file only contains one namespace definition.

If you have multiple namespaces, and you want to have global namespace, then use namespace keyword followed by no names.

 

Namespace direct access

PHP 5
<?php
namespace myproject;

/*
  include (import) necessary .php files here.
*/

function greet(){
    print('Hi!');
}

// Unqualified name
greet();  // Hi!

// Qualified name
spanish\greet();   // Hola!

// Fully qualified name
\myproject\english\greet();   // Hello!

There are 3 ways to point directly to something inside a namespace without importing it:

  1. Unqualified name
  2. Qualified name
  3. Fully qualified name
 

Importing

PHP 5
/*
  include (import) necessary .php files here.
*/

use myproject\spanish;

spanish\greet();    // Hola!

Namespaces can be imported with the use operator.

You can have multiple imports separated by comma or have multiple use statements in a file.

 

Aliasing

PHP 5
/*
  include (import) necessary .php files here.
*/

use myproject\spanish as SPA;

SPA\greet();     // Hola!

Namespaces can be aliased (renamed) when being imported.

 

Importing Single Item

PHP 5
/*
  include (import) necessary .php files here.
*/

use function myproject\spanish\greet;

greet();    //Hola!

You can import directly a class, function or const.

There is no wildcard imports like Java in PHP and you should import each item one by one.

Class:

use Namespace\Path\To\MyClass;

Function:

use function Namespace\Path\To\myFunction;

Const:

use const Namespace\Path\To\myConst;
 

Importing Multiple Items

PHP 7
// Class
use Namespace\Path\To\{MyClass1 as cl1, MyClass2};

// Function
use function Namespace\Path\To\{myFunction1 as f1, MyFunction2};

// Const 
use const Namespace\Path\To\{myConst1 as co1, MyConst2};

PHP 7 introduced a new way of importing classes, functions and consts, grouped together in a single use statement.