PHP Reference ยท Language Constructs and Statements
PHP namespace and use
Reference for PHP namespace and use: declare namespaces, import classes, functions and constants, alias names, group use and fallback rules.
Namespaces give names a prefix so that two libraries can both define a class called User or a function called format() without clashing. A namespace statement at the top of a file puts everything declared below it into that namespace; the use statement imports names from other namespaces so you can write them short.
Namespaces affect classes, interfaces, traits, enums, functions and constants (declared with const). They do not affect variables or define() constants.
namespace App\Models; use App\Support\Str; use InvalidArgumentException as BadInput; final class User {}
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
namespace Vendor\Package; namespace Vendor\Package { /* braced form */ } use Vendor\Package\ClassName; use Vendor\Package\ClassName as Alias; use function Vendor\Package\helper; use const Vendor\Package\LIMIT; use Vendor\Package\{ClassA, ClassB as B, function helper, const LIMIT};
| Name | Description |
|---|---|
namespace Name; |
Must be the first statement in the file after an optional declare. Every following declaration belongs to Name. |
namespace Name { } |
Braced form for several namespaces in one file. Cannot be mixed with the unbraced form. |
use Name; |
Imports a class, interface, trait or enum. The alias is the last segment. |
use function / use const |
Imports a function or constant. |
as Alias |
Gives the import another name. |
Errors: importing a name that clashes with a class already declared in the file is a fatal error. Using a class that cannot be found throws Error ("Class ... not found") when it is first used.
Usage notes
- Name kinds.
Foois unqualified,Sub\Foois qualified (relative to the current namespace) and\Sub\Foois fully qualified. A leading backslash always starts at the global namespace. - Fallback applies only to functions and constants. An unqualified call such as
strlen()inside a namespace is tried in the namespace first, then globally. Classes have no fallback, so write\Exceptionoruse Exception;. - Performance. Writing
\strlen(...)oruse function strlen;lets the engine skip the namespace lookup and is a common micro-optimization. useimports are per file and are resolved at compile time. They are not inherited by included files. Do not confuse them withusein closures (function () use ($x)) and in classes (use SomeTrait;).::classreturns the fully qualified name as a string without loading the class:Invoice::class.- Dynamic names need the full name as a string, without a leading backslash:
new $class. Imports do not apply to strings. - PSR-4 convention. Namespace segments map to directories, one class per file, which lets an autoloader find classes; see Autoloading.
namespace\Foois an explicit relative name that means "current namespace". Rarely needed.- Reserved names. Type names such as
int,stringandobjectcannot be used as class names, in any namespace.
Related
- Magic constants (
__NAMESPACE__), Autoloading,const and define, Closures and arrow functions - Lessons: Namespaces, Autoloading and Composer