← Back to PHP Course | Chapter 8: OOP | Lesson 12 of 14

PHP Namespaces

A namespace is like a family name that separates two people who share a first name. It stops two classes with the same name from getting mixed up.
Syntax
php
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
<?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();
?>

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
<?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();
?>

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
<?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();
}
?>

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
<?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();
?>

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
<?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();
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Placing namespace after other code, when it must be the first statement in the file.
  2. Forgetting the leading backslash for global classes such as \Exception inside a namespace.
  3. Forgetting the use statement, so PHP looks for the class in the current namespace.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.