Namespaces
Designed to prevent name collisions between classes from different packages. Very long names in the code to refer to classes or functions are not needed anymore.
require("com/yahoo/MyClass.php");
echo MyClass::A; // 1
require("com/google/MyClass.php");
echo MyClass::A; // Fatal error: Cannot redeclare class MyClas
With namespaces, you can avoid conflicts between different libraries.
require("com/yahoo/MyClass.php");
require("com/google/MyClass.php");
echo com\yahoo\MyClass::A; // 1
echo com\google\MyClass::A; // 2
/com/yahoo/MyClass.php
namespace com\yahoo;
class MyClass
{
const A = 1;
}
/com/google/MyClass.php
namespace com\google;
class MyClass
{
const A = 2;
}
Autoload
With autoload, no require needed.
function classAutoLoader($class) {
$tmp = explode('', $class);
require join("/", $tmp) . '.php';
}
spl_autoload_register("classAutoLoader"); // Look here
echo com\yahoo\MyClass::A; // Output 1
echo com\google\MyClass::A; // Output 2
Defining
A namespace is defined with the namespace keyword and a name. It must be declared before any other code. Once defined, the scope of the namespace applies to the entire file.
namespace MyNamespace;
Brackets
A namespace may alternatively be defined by wrapping brackets around its contents. No code is allowed outside the brackets. These two methods cannot be mixed, so choose one and stick to it.
namespace MyNamespace {
// ...
}
Nested
Nested namespaces can be defined by separating each level with a backslash.
namespace MyProject\Blog\Admin;
Multiple
Multiple namespaces may be defined in a single file. The scope of the preceding namespace ends once the next namespace appears.
namespace MyProjectBlog;
// ...
namespace MyProjectStore;
// ...
Global
For global namespace, use the namespace keyword without a name. This feature can only be used in conjunction with the curly bracket syntax.
namespace MyProject {
// MyProject namespace ...
}
namespace { // Look here
// Global namespace ...
}
Identifiers
Any code can appear within the namespace. But only classes, interfaces, functions and constants are affected by it. These identifiers can be referenced in one of three ways.Fully-qualified
A fully-qualified name is like an absolute path in a file system.
$a = new \myRoot\myLibrary\classes\myClass;
Qualified
A qualified name is like a relative path in a file system.
$a = new myRoot\myLibrary\classes\myClass;
Unqualified
It refers to the current namespace only. If is not found in the current scope, It will just refer to the global namespace.
$a = new myClass;
Global
To reference classes in the global namespace, you must begin with backslash. The functions do not need backslash.
namespace myLibrary\classes;
$obj = new ArrayObject(); // Fatal error: Class not found
$obj = new \ArrayObject(); // Look here
echo strlen("Hello World"); // Corect, resolves to native function
Aliases
In a large application it can become cumbersome to write out identifier names. You can import namespaces, classes and interfaces using the use keyword. This allows them to be referenced by an alias instead of their full name.
function classAutoLoader($class) {
$tmp = explode('', $class);
require join("/", $tmp) . '.php';
}
spl_autoload_register("classAutoLoader");
use com\yahoo as y; // Look Here
use com\google as g;
echo y\MyClass::A; // Outputs 1
echo g\MyClass::A; // Outputs 2
namespace com\yahoo;
class MyClass
{
const A = 1;
}
namespace com\google;
class MyClass
{
const A = 2;
}
PHP namespaces support three kinds of aliasing or importing.
Aliasing a class name, aliasing an interface name, and aliasing a namespace name.
Note that importing a function or constant is not supported.
Fully-qualified
Only unqualified and qualified names are affected by aliases. Fully-qualified names are not. Therefore, the following will not be affected by the defined aliases.
namespace myLibrary\classes {
require_once('myLibrary/classes/myClass.php');
use myLibrary\classes\myClass as Another;
$a = new Another;
$b = new \myLibrary\classes\myClass;
// fully-qualified names are not affected by alias
}
PHP lets you separate multiple namespace declarations with commas.
use com\yahoo as y, com\google as g;
Last update: 382 days ago