(PHP 5 >= 5.3.0, PHP 7, PHP 8)
This FAQ is split into two sections: common questions, and some specifics of implementation that are helpful to understand fully.
First, the common questions.
\my\name
or \name
resolve? my\name
resolve? name
resolve? name
resolve? There are a few implementation details of the namespace implementations that are helpful to understand.
No. Namespaces do not affect any existing code in any way, or any as-yet-to-be-written code that does not contain namespaces. You can write this code if you wish:
This is functionally equivalent to:
Example #4 Accessing internal classes, functions or constants in namespaces
<?php namespace foo; class MyClass {} // using a class from the current namespace as a type hint function test(MyClass $typehintexample = null) {} // another way to use a class from the current namespace as a type hint function test(\foo\MyClass $typehintexample = null) {} // extending a class from the current namespace class Extended extends MyClass {} // accessing a global function $a = \globalfunc(); // accessing a global constant $b = \INI_ALL; ?>
\my\name
or \name
resolve? Names that begin with a \
always resolve to what they look like, so \my\name
is in fact my\name
, and \Exception
is Exception
.
my\name
resolve? Names that contain a backslash but do not begin with a backslash like my\name
can be resolved in 2 different ways.
If there is an import statement that aliases another name to my
, then the import alias is applied to the my
in my\name
.
Otherwise, the current namespace name is prepended to my\name
.
Example #6 Qualified names
name
resolve? Class names that do not contain a backslash like name
can be resolved in 2 different ways.
If there is an import statement that aliases another name to name
, then the import alias is applied.
Otherwise, the current namespace name is prepended to name
.
name
resolve? Function or constant names that do not contain a backslash like name
can be resolved in 2 different ways.
First, the current namespace name is prepended to name
.
Finally, if the constant or function name
does not exist in the current namespace, a global constant or function name
is used if it exists.
Example #8 Unqualified function or constant names
<?php namespace foo; use blah\blah as foo; const FOO = 1; function my() {} function foo() {} function sort(&$a) { \sort($a); // calls the global function "sort" $a = array_flip($a); return $a; } my(); // calls "foo\my" $a = strlen('hi'); // calls global function "strlen" because "foo\strlen" does not exist $arr = array(1,3,2); $b = sort($arr); // calls function "foo\sort" $c = foo(); // calls function "foo\foo" - import is not applied $a = FOO; // sets $a to value of constant "foo\FOO" - import is not applied $b = INI_ALL; // sets $b to value of global constant "INI_ALL" ?>
The following script combinations are legal:
file1.php
another.php
file2.php
There is no name conflict, even though the class MyClass
exists within the my\stuff
namespace, because the MyClass definition is in a separate file. However, the next example causes a fatal error on name conflict because MyClass is defined in the same file as the use statement.
PHP does not allow nesting namespaces
However, it is easy to simulate nested namespaces like so:It is very important to realize that because the backslash is used as an escape character within strings, it should always be doubled when used inside a string. Otherwise there is a risk of unintended consequences:
Inside a single-quoted string, the backslash escape sequence is much safer to use, but it is still recommended practice to escape backslashes in all strings as a best practice. Any undefined constant that is unqualified like FOO
will produce a notice explaining that PHP assumed FOO
was the value of the constant. Any constant, qualified or fully qualified, that contains a backslash will produce a fatal error if not found.
Example #10 Undefined constants
Any attempt to define a namespaced constant that is a special, built-in constant results in a fatal error
© 1997–2020 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://www.php.net/manual/en/language.namespaces.faq.php