Skip to main content

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:

Prompt
Create a PHP namespace called Html with a Table class that has a $title property, a $rows property, and an info() method that echoes the title and number of rows. Create an instance of the Table class and set its title to "My table" and rows to 5, then call the info() method.
Output: PHP script

Declare a namespace

Put the namespace keyword at the top of the file:

index.php
<?php
namespace Html;
?>

Invalid namespace declaration

This is invalid because other code comes before the namespace declaration:

index.php
<?php
echo "Hello World!";
namespace Html;
...
?>

Nested namespace syntax

Here, Html is nested inside a namespace called Code:

index.php
<?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:

Prompt
Write PHP code that defines Table and Row classes in the Html namespace, creates an instance of each from the global namespace by specifying the namespace with the class name, and prints each object's full class name with get_class().
Output: PHP script

Example

After declaring the Html namespace, the classes defined in it no longer need the Html\ qualifier. get_class() still reports their full names:

Prompt
Write PHP code inside the Html namespace that defines Table and Row classes, creates instances of them without needing the Html\ qualifier, and prints each object's full class name with get_class().
Output: PHP script

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:

Prompt
Write PHP code that defines a Table class in the Html namespace, gives the namespace an alias of H using the use keyword, creates a new Table instance using the alias, and prints the object's full class name with get_class().
Output: PHP script

Example

Here, T becomes an alias for the Html\Table class, so new T() creates an Html\Table object:

Prompt
Write PHP code that defines a Table class in the Html namespace, gives the Html\Table class an alias of T using the use keyword, creates a new instance using the alias, and prints the object's full class name with get_class().
Output: PHP script