PHP namespace Keyword
last modified November 25, 2026
The PHP namespace keyword declares a named scope for classes, interfaces, functions, and constants. Namespaces prevent name collisions between libraries and give the code a structure that autoloaders can follow.
This article shows the two declaration forms, nested names, name resolution, and access to the global namespace. Imports are covered in a separate article.
Basic definitions
A namespace is a named container for classes, interfaces, functions, and constants. It works as a prefix added to each name, so two libraries may both define a Logger class as long as they live in different namespaces.
A namespace is declared with namespace MyApp; as the first statement of a file, after an optional declare. The bracketed form namespace MyApp { ... } can declare several namespaces in one file. Code that is not inside any namespace belongs to the global namespace.
A name is written in one of three ways. The fully qualified name starts with a backslash, as in \MyApp\Logger. A qualified name such as Storage\Disk is relative to the current namespace. An unqualified name such as Disk refers to the current namespace, except for functions and constants, which fall back to the global namespace when no namespaced version exists.
The use statement imports a name and is covered in the PHP use keyword article. This article concentrates on declaring namespaces and referring to names without imports.
Declaring a namespace
The first example declares one namespace and defines a class inside it.
<?php
declare(strict_types=1);
namespace MyApp;
class Greeter
{
public function greet(): string
{
return "Hello from " . __NAMESPACE__;
}
}
$greeter = new Greeter();
echo $greeter->greet(), "\n";
echo Greeter::class, "\n";
The magic constant __NAMESPACE__ returns the current namespace as a string, here MyApp. The expression Greeter::class returns the fully qualified class name without a leading backslash, MyApp\Greeter. Inside the namespace the short name Greeter is enough; outside it, the class must be referred to as \MyApp\Greeter.
$ php ns_declare.php Hello from MyApp MyApp\Greeter
Nested namespaces
A namespace name may contain several levels separated by backslashes. The levels do not have to be declared one by one.
<?php
declare(strict_types=1);
namespace MyApp\Storage;
class Disk
{
public function label(): string
{
return "local disk";
}
}
echo "namespace: ", __NAMESPACE__, "\n";
echo "class: ", Disk::class, "\n";
echo (new Disk())->label(), "\n";
The file declares MyApp\Storage even though MyApp is never declared on its own. The namespace is only a prefix, not a directory object, so MyApp exists implicitly. By convention the prefix mirrors the directory structure of the project, which is what PSR-4 autoloading expects.
$ php ns_nested.php namespace: MyApp\Storage class: MyApp\Storage\Disk local disk
Several namespaces in one file
The bracketed syntax allows more than one namespace per file. A file that uses it must wrap even global code in a namespace { ... } block.
<?php
declare(strict_types=1);
namespace MyApp {
class Logger
{
public function tag(): string
{
return "application";
}
}
}
namespace Other {
class Logger
{
public function tag(): string
{
return "library";
}
}
}
namespace {
$app = new MyApp\Logger();
$lib = new Other\Logger();
echo $app->tag(), "\n";
echo $lib->tag(), "\n";
}
Both classes have the short name Logger, but their fully qualified names differ, so there is no collision. The global block refers to them as MyApp\Logger and Other\Logger. Real projects keep one namespace per file; the bracketed form is used here so that each example is a single runnable script.
$ php ns_two.php application library
The global namespace
Inside a namespace, an unqualified function call first looks for a function of that name in the current namespace. If none exists, PHP falls back to the global function. A leading backslash skips the search and calls the global function directly.
<?php
declare(strict_types=1);
namespace MyApp;
function strlen(string $text): int
{
return \strlen($text) * 2;
}
function describe(string $text): string
{
return "length " . strlen($text) . " or " . \strlen($text);
}
echo describe("abcd"), "\n";
The file defines MyApp\strlen, which doubles the result of the built-in strlen. The unqualified call inside describe reaches the namespaced version and returns 8, while \strlen calls the global function and returns 4. Class names do not fall back in this way: an unqualified new Logger() looks only in the current namespace and fails if the class is not there.
$ php ns_global.php length 8 or 4
Namespaced constants and functions
Classes are not the only members of a namespace. Functions and constants declared there belong to it and are referenced with the same prefix.
<?php
declare(strict_types=1);
namespace MyApp\Utils;
const VERSION = "2.0";
function greet(string $name): string
{
return "Hello, $name";
}
class Report
{
public function header(): string
{
return greet("reader") . " (" . VERSION . ")";
}
}
echo (new Report())->header(), "\n";
echo \MyApp\Utils\VERSION, "\n";
echo \MyApp\Utils\greet("Ada"), "\n";
The constant VERSION and the function greet live in MyApp\Utils, next to the class. Code inside the namespace refers to them by their short names. Code outside uses the fully qualified forms \MyApp\Utils\VERSION and \MyApp\Utils\greet(), or imports them with use const and use function. Constants are case-sensitive, while function and class names are not.
$ php ns_utils.php Hello, reader (2.0) 2.0 Hello, Ada
Imports and autoloading
A use statement only creates an alias for a name at compile time. It does not include a file and does not load the class by itself. Loading happens when the name is first used and an autoloader has been registered, usually by Composer.
The syntax for importing classes, functions, and constants, including group imports and aliases, is described in the PHP use keyword article. Aliasing with as is covered in PHP as keyword. The broader tutorial PHP Namespaces shows a complete project layout.
Best practices
The rules below keep namespace names predictable and let PSR-4 autoloading work without extra configuration.
- Keep one namespace per file and let the namespace match the directory path, so that PSR-4 autoloading can find the class.
- Start the namespace with a vendor name, for example
Acme\Shop, to avoid collisions with other libraries. - Do not put executable statements in a file that only declares a namespace; keep it to declarations.
- Avoid redefining global functions such as
strlenunless the shadowing is deliberate and documented. - Prefer imports over long fully qualified names, but only where the shorter name stays unambiguous.
Source
PHP manual: namespace resolution rules
This article explained how PHP namespaces are declared, how nested names and the global namespace are resolved, and how functions and constants live inside a namespace. Continue with PHP use keyword for imports, PHP as keyword for aliases, and PHP Namespaces for a full project example.
Author
List all PHP tutorials.