Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A PHP namespace declaration is a language construct used to encapsulate classes, interfaces, traits, enums, functions, and constants into distinct, hierarchical logical scopes. By defining a namespace, you alter the current execution context, requiring the PHP engine to resolve identifiers relative to that declared scope rather than the global space. The declaration is executed using the namespace keyword.

Core Rules of Declaration

  1. Positioning: A namespace declaration must be the very first statement in the PHP script. The only PHP construct permitted to precede a namespace declaration is a declare statement (e.g., declare(strict_types=1);). Whitespace and comments are permitted inside the <?php tag before the keyword, but any non-PHP code, HTML, or whitespace output before the opening <?php tag will result in a fatal parser error.
  2. Naming Conventions: Namespace identifiers must follow standard PHP naming rules (starting with a letter or underscore, followed by any number of letters, numbers, or underscores).

Standard Declaration

A standard namespace is declared using the namespace keyword followed by the identifier and a terminating semicolon.
<?php
declare(strict_types=1);

namespace App;

class Router {
    // Class is scoped to App\Router
}

Sub-Namespace Declaration

PHP supports hierarchical namespace architectures. Sub-namespaces are declared by separating identifiers with the backslash character (\).
<?php
namespace App\Http\Controllers;

class UserController {
    // Class is scoped to App\Http\Controllers\UserController
}

Multiple Namespaces in a Single File

While generally discouraged by PSR-4 autoloading standards, PHP permits declaring multiple namespaces within a single file. This can be achieved using either unbracketed or bracketed syntax. Bracketed Syntax (Recommended for multiple namespaces): When using bracketed syntax, all code belonging to the namespace must be enclosed within the curly braces. No code may exist outside of these brackets, with the explicit exception of a valid declare statement.
<?php
declare(strict_types=1);

namespace App\Models {
    class User {}
}

namespace App\Services {
    class UserService {}
}
Unbracketed Syntax: The unbracketed syntax changes the active namespace for all subsequent code until the next namespace declaration is encountered.
<?php
namespace App\Models;
class User {}

namespace App\Services;
class UserService {}

Global Namespace Declaration

If a file containing multiple namespaces requires code to be executed in the global scope, it must be declared using the bracketed syntax without an identifier. Mixing bracketed and unbracketed syntax in the same file is invalid and will throw a fatal error.
<?php
namespace App\Utils {
    class Logger {}
}

namespace {
    // This code executes in the global scope
    $logger = new \App\Utils\Logger();
}
Master PHP with Deep Grasping Methodology!Learn More