PHP Namespaces
In this page:
namespace Vendor\Package;
class ClassName { }
use Vendor\Package\ClassName; // import in another file
$object = new ClassName();
What are Namespaces?
A namespace groups related classes, functions, and constants under a common prefix, solving naming collisions that would otherwise occur if two different libraries both defined a class called Response.
Example: What are Namespaces?
<?php
// Declare the namespace `App\Models`
namespace App\Models;
// Define the class `Response`
class Response {
// Define the function `send` with no parameters
function send() {
// Print "App\\Models\\Response" to the output
echo "App\\Models\\Response";
}
}
// Create a new `Response` instance, stored in `$r`
$r = new Response();
$r->send();
?>
Login to try C/C++/Java/PHP code in the editor
Declaring Namespaces
You declare a namespace with the namespace keyword as the very first statement in a file, like namespace App\Models;, and PHP treats everything after that declaration as belonging to that namespace.
Example: Declaring Namespaces
<?php
// Declare the namespace `App\Models`
namespace App\Models;
// Define the class `User`
class User {
// Define the constructor `__construct` with no parameters
function __construct() {
// Print `"User in namespace: " . __NAMESPACE__` to the output
echo "User in namespace: " . __NAMESPACE__;
}
}
new User();
?>
Login to try C/C++/Java/PHP code in the editor
Using Namespaces
The use keyword imports a namespaced class into the current file so you can refer to it by its short name instead of writing out the full namespaced path every time you use it.
Example: Using Namespaces
<?php
namespace App\Models {
// Define the class `User`
class User {
// Define the function `hello` with no parameters
function hello() { echo "Hello from User"; }
}
}
namespace App {
// Import `App\Models\User` into this file
use App\Models\User;
// Create a new `User` instance, stored in `$user`
$user = new User();
$user->hello();
}
?>
Login to try C/C++/Java/PHP code in the editor
The use Keyword
A leading backslash, like \DateTime, refers to a class in the global namespace, which matters when you're inside a custom namespace and need to explicitly reach a built-in PHP class rather than a same-named class of your own.
Example: The use Keyword
<?php
// Declare the namespace `App`
namespace App;
// Define the class `DateTime`
class DateTime {
// Define the function `today` with no parameters
function today() {
// Return `(new \DateTime())->format("Y-m-d")` from this function
return (new \DateTime())->format("Y-m-d");
}
}
// Create a new `DateTime` instance, stored in `$d`
$d = new DateTime();
// Print `$d->today()` to the output
echo $d->today();
?>
Login to try C/C++/Java/PHP code in the editor
Sub-Namespaces
Namespaces are the foundation Composer's autoloading relies on, mapping namespace paths to folder structures so that classes from third-party packages can be loaded automatically without manual require statements.
Example: Sub-Namespaces
<?php
// Declare the namespace `App\Services\Payment`
namespace App\Services\Payment;
// Define the class `Gateway`
class Gateway {
// Define the function `charge` with no parameters
function charge() {
// Print "Charging via " . __NAMESPACE__ . "\\Gateway" to the output
echo "Charging via " . __NAMESPACE__ . "\\Gateway";
}
}
(new Gateway())->charge();
?>
Login to try C/C++/Java/PHP code in the editor
- Placing
namespaceafter other code, when it must be the first statement in the file. - Forgetting the leading backslash for global classes such as
\Exceptioninside a namespace. - Forgetting the
usestatement, so PHP looks for the class in the current namespace.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: