(PHP 5 >= 5.3.0, PHP 7, PHP 8)
For the purposes of these resolution rules, here are some important definitions:
This is an identifier without a namespace separator, such as Foo
This is an identifier with a namespace separator, such as Foo\Bar
This is an identifier with a namespace separator that begins with a namespace separator, such as \Foo\Bar
. The namespace \Foo
is also a fully qualified name.
This is an identifier starting with namespace
, such as namespace\Foo\Bar
.
Names are resolved following these resolution rules:
\A\B
resolves to A\B
. namespace
replaced by the current namespace. If the name occurs in the global namespace, the namespace\
prefix is stripped. For example namespace\A
inside namespace X\Y
resolves to X\Y\A
. The same name inside the global namespace resolves to A
. A\B\C
is imported as C
, the name C\D\E
is translated to A\B\C\D\E
. C\D\E
inside namespace A\B
, resolves to A\B\C\D\E
. use A\B\C;
a usage such as new C()
resolves to the name A\B\C()
. Similarly, after use function A\B\foo;
a usage such as foo()
resolves to the name A\B\foo
. new C()
inside namespace A\B
resolves to name A\B\C
. A\B
, here is how a call to function foo()
is resolved: A\B\foo()
. foo()
. Example #1 Name resolutions illustrated
<?php
namespace A;
use B\D, C\E as F;
// function calls
foo(); // first tries to call "foo" defined in namespace "A"