Nomes são resolvidos seguindo estas regras de resolução:
C\D\e()
é traduzida para A\B\C\D\e()
.
new C()
é
traduzido para new A\B\C()
.
new C()
é resolvida:
new \C()
tem que ser usado.
new A\B\C()
refere-se a classe
C do namespace A\B.
Exemplo #1 Ilustrando resolução de nomes
<?php
namespace A;
// function calls
foo(); // first tries to call "foo" defined in namespace "A"
// then calls internal function "foo"
\foo(); // calls function "foo" defined in global scope
// class references
new B(); // first tries to create object of class "B" defined in namespace "A"
// then creates object of internal class "B"
new \B(); // creates object of class "B" defined in global scope
// static methods/namespace functions from another namespace
B\foo(); // first tries to call function "foo" from namespace "A\B"
// then calls method "foo" of internal class "B"
\B\foo(); // first tries to call function "foo" from namespace "B"
// then calls method "foo" of class "B" from global scope
// static methods/namespace functions of current namespace
A\foo(); // first tries to call function "foo" from namespace "A\A"
// then tries to call method "foo" of class "A" from namespace "A"
// then tries to call function "foo" from namespace "A"
// then calls method "foo" of internal class "A"
\A\foo(); // first tries to call function "foo" from namespace "A"
// then calls method "foo" of class "A" from global scope
?>