This post covers the Namespaces section of the PHP Basics chapter when studying for the Zend PHP 7 Certification.
Namespaces are a method of grouping related PHP code elements within a library or application.
Namespaces are use to solve a couple of problems including naming collisions, which most likely occur when code from multiple people is included in a project. For example, you could create a class or method that has exactly the same name as someone else’s class or method.
Although any valid PHP code can be contained within a namespace, only the following types of code are affected by namespaces:
Aside from the one exception of using the declare
keyword, a file containing a namespace must declare the namespace at the top of the file before any other code.
<?php
namespace test;
const SOME_CONST = 1;
class someClass { ... }
function someFunc { ... }
A namespace name can also be defined with sub-levels.
<?php
namespace MyProject\Sub\Level;
const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */ }
The above example creates constant MyProject\Sub\Level\CONNECT_OK
, class MyProject\Sub\Level\Connection
and function MyProject\Sub\Level\connect
.
Unless a namespace is defined, classes and functions are declared within the global scope.
It is strongly discouraged as a coding practice to combine multiple namespaces into the same file. However, if you need to then use bracketed syntax:
<?php
namespace MyProject {
const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */ }
}
namespace AnotherProject {
const CONNECT_OK = 1;
class Connection { /* ... */ }
function connect() { /* ... */ }
}
As seen in the constants chapter, the __NAMESPACE__
magic constant contains the current namespace.
<?php
namespace somenamespace;
echo __NAMESPACE__; // Outputs: somenamespace
Namespaces can be imported with the use
operator. Note that you can also alias the namespace by using the as
keyword.
<?php
namespace foo;
use My\Full\Classname as Another;
Without any namespace definition, all class and function definitions are placed into the global space. Prefixing a name with \
will specify that the name is required from the global space even in the context of the namespace.
<?php
namespace A\B\C;
class Exception extends \Exception {}
$a = new Exception('Some exception'); // $a is an object of class A\B\C\Exception
$b = new \Exception('Some exception'); // $b is an object of class Exception
As of PHP 7.0, classes, functions and constants being imported from the same namespace can be grouped together in a single use
statement.
<?php
// Pre PHP 7 code
use some\namespace\ClassA;
use some\namespace\ClassB;
use some\namespace\ClassC as C;
use function some\namespace\fn_a;
use function some\namespace\fn_b;
use function some\namespace\fn_c;
use const some\namespace\ConstA;
use const some\namespace\ConstB;
use const some\namespace\ConstC;
// PHP 7+ code
use some\namespace\{ClassA, ClassB, ClassC as C};
use function some\namespace\{fn_a, fn_b, fn_c};
use const some\namespace\{ConstA, ConstB, ConstC};
View the other sections:
Note: This article is based on PHP version 7.0.