Namespaces
A namespace is a prefix that turns a short class name into a full one. It is how two parts of a PHP program can both use the name Cart without a clash.
Why namespaces exist
Two libraries can both define a class named Cart, and PHP allows each name only once. Namespaces put classes into named groups, so Acme carts and Beta carts coexist. The deep dive lives at Namespaces in PHP.
<?php namespace Acme\Shop { class Cart { public function label(): string { return "acme cart"; } } } namespace Beta\Shop { class Cart { public function label(): string { return "beta cart"; } } } namespace { $a = new \Acme\Shop\Cart(); $b = new \Beta\Shop\Cart(); echo $a->label(), PHP_EOL; echo $b->label(), PHP_EOL; }
php why.php
acme cart
beta cart
Both classes are called Cart, and the namespace is what tells them apart. This example puts both in one file, in the bracketed form, so that both fit here; a real project gives each class its own file, as the next section does.
Declare a namespace
The namespace declaration must be the first statement in the file, and everything declared below it belongs to that group. One namespace per file keeps the mapping between files and names obvious.
<?php namespace Shop; class Cart { public function total(): float { return 0.0; } } $cart = new Cart(); var_dump($cart->total());
php declare.php
float(0)
The class is now Shop\Cart to the rest of the program, and the short name Cart works inside the same namespace without any import.
Import with use
The use statement imports a class under its short name, so the code below reads cleanly. When two imports share a short name, the as keyword gives one of them an alias.
<?php namespace Shop { class Cart { } class Order { } } namespace { use Shop\Cart; use Shop\Order as CustomerOrder; $cart = new Cart(); $order = new CustomerOrder(); echo get_class($cart), PHP_EOL; echo get_class($order), PHP_EOL; }
php imports.php
Shop\Cart
Shop\Order
The alias is only a name in this file: the second line of the output still shows the real class, Shop\Order.
Back to global with backslash
A leading backslash escapes to the global space: \DateTime always means the built-in class, even inside a namespaced file. Remember that unqualified function calls fall back to global functions, while unqualified class names never do, so qualify shared classes explicitly.
<?php namespace Shop; function make(): \DateTime { return new \DateTime("2026-01-01"); } echo get_class(make()), PHP_EOL; echo make()->format("Y-m-d"), PHP_EOL;
php global.php
DateTime
2026-01-01
Without the backslash, new DateTime() would look for Shop\DateTime and stop with Class "Shop\DateTime" not found. The function call make() needs no prefix, because a plain function name falls back to the global one when the namespace has none.
Exercises
- Add a third namespace, Gamma\Shop, with its own Cart to why.php and print its label.
- Call total() from outside the namespace of declare.php without an import. Which name must the call use?
- Give the Order import of imports.php a different alias and print get_class() again. What changes?
- Remove the backslash before DateTime in global.php and run it. Which class does PHP look for, and what does the error say?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why do two libraries that both define a
Cartclass need namespaces?Answer
PHP allows a class name to be declared only once
A class name is unique in its namespace, so the prefix is what lets two Cart classes live in one program.
-
Where does the
namespacedeclaration of a file go?Answer
At the top, as the first statement
The declaration opens the group that the rest of the file belongs to, so it comes first.
-
What does
use Shop\Order as CustomerOrder;do?Answer
It imports the class under the local name CustomerOrder
The alias is a local short name in this file; the class keeps its real name everywhere else.
-
What does a leading backslash in
\DateTimemean?Answer
The root or global namespace
The backslash at the start points at the global namespace, so the built-in class is found from any namespace.
-
What does this program print?
<?php namespace Shop; class Cart { } $c = new Cart(); echo get_class($c);Answer
Shop\Cart
get_class returns the full name of the class, and the namespace is part of it.
-
What does this program print?
<?php namespace Shop; echo __NAMESPACE__;Answer
Shop
__NAMESPACE__ holds the name of the current namespace as a string.
-
What does this program print?
<?php namespace Shop; echo get_class(new \DateTime());Answer
DateTime
The leading backslash names the global DateTime class, so get_class reports DateTime.
-
What does this program print?
<?php namespace Shop; function label(): string { return "shop function"; } echo label();Answer
shop function
The call is resolved in the current namespace, where label() is defined, so it returns its string.
-
What happens when a namespaced file calls
new DateTime()without a backslash or an import?Answer
PHP looks for Shop\DateTime and fails with a class-not-found error
Unqualified class names never fall back to the global namespace, so the namespaced name is the one searched.
-
Which name does a plain function call use when the current namespace has no such function?
Answer
The global function of that name
Functions fall back to the global namespace; only classes and constants need the explicit prefix.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev