PHP Namespaces
A namespace gives related code a shared name. It keeps classes, interfaces, functions and constants from clashing with others that have the same name.
That protection matters more as your code grows, or once you combine code from several sources.
Why Use Namespaces?
- They prevent name clashes, which become more likely in larger projects
- They sort your code into logical groups
- They keep your own code apart from library code
- They let more than one class share a name safely
Say one set of classes describes an HTML table (Table, Row, Cell) and another set describes furniture (Table, Chair, Bed). Putting each set in its own namespace keeps the two Table classes from being confused.
Declaring a Namespace
A namespace is declared at the top of a PHP file: write namespace and then the name you chose. The snippets in this section declare one called Html.
Note: Placement is strict. The declaration has to come before everything else in the file, so code placed ahead of it is invalid.
From that point on, every class, interface, function and constant declared in the file belongs to the Html namespace.
You can also nest namespaces for finer organization.
Example
Here, a Table class is created inside the Html namespace:
Output: PHP script
<?php
namespace Html;
class Table {
public $title = "";
public $rows = 0;
public function info() {
echo "<p>$this->title has $this->rows rows.</p>";
}
}
$table = new \Html\Table();
$table->title = "My table";
$table->rows = 5;
?>
<!DOCTYPE html>
<html>
<body>
<?php
$table->info();
?>
</body>
</html>
Declare a namespace
Put the namespace keyword at the top of the file:
<?php
namespace Html;
?>
Invalid namespace declaration
This is invalid because other code comes before the namespace declaration:
<?php
echo "Hello World!";
namespace Html;
...
?>
Nested namespace syntax
Here, Html is nested inside a namespace called Code:
<?php
namespace Code\Html;
?>
Using Namespaces
All the code after a namespace declaration runs inside that namespace, so it can create the namespace's classes by their short names, with no qualifier. Code outside the namespace has to attach the namespace to the class name.
If you work with many classes from one namespace at once, the namespace keyword saves typing:
Example
So that the example can run, the classes are defined in a namespace Html { } block, and the rest of the code sits in a namespace { } block, which is the global namespace. From there, each class name carries the Html\ prefix, and get_class() prints the full name of each object's class:
Output: PHP script
<?php
namespace Html {
class Table {}
class Row {}
}
namespace {
$table = new Html\Table();
$row = new Html\Row();
echo get_class($table) . "\n";
echo get_class($row) . "\n";
}
?>
Example
After declaring the Html namespace, the classes defined in it no longer need the Html\ qualifier. get_class() still reports their full names:
Output: PHP script
<?php
namespace Html;
class Table {}
class Row {}
$table = new Table();
$row = new Row();
echo get_class($table) . "\n";
echo get_class($row) . "\n";
?>
Namespace Alias
An alias is a shorter name you pick for a namespace or class, and it makes the code easier to write. You set one up with the use keyword:
Example
Here, H becomes an alias for the Html namespace, so H\Table refers to the same class as Html\Table:
Output: PHP script
<?php
namespace Html {
class Table {}
}
namespace {
use Html as H;
$table = new H\Table();
echo get_class($table) . "\n";
}
?>
Example
Here, T becomes an alias for the Html\Table class, so new T() creates an Html\Table object:
Output: PHP script
<?php
namespace Html {
class Table {}
}
namespace {
use Html\Table as T;
$table = new T();
echo get_class($table) . "\n";
}
?>