PHP.nl

Name resolution rules

Name resolution rules

For the purposes of these resolution rules, here are some important definitions:

Namespace name 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 . The namespace
    is also a fully qualified name.
  `\Foo\Bar``\Foo`


   This is an identifier starting with , such as
   .
  `namespace``namespace\Foo\Bar`

Names are resolved following these resolution rules:

  1. Fully qualified names always resolve to the name without leading namespace separator. For instance resolves to . \A\B``A\B
  2. Relative names always resolve to the name with replaced by the current namespace. If the name occurs in the global namespace, the prefix is stripped. For example inside namespace resolves to . The same name inside the global namespace resolves to . namespace``namespace\``namespace\A``X\Y``X\Y\A``A
  3. For qualified names the first segment of the name is translated according to the current class/namespace import table. For example, if the namespace is imported as , the name is translated to . A\B\C``C``C\D\E``A\B\C\D\E
  4. For qualified names, if no import rule applies, the current namespace is prepended to the name. For example, the name inside namespace , resolves to . C\D\E``A\B``A\B\C\D\E
  5. For unqualified names, the name is translated according to the current import table for the respective symbol type. This means that class-like names are translated according to the class/namespace import table, function names according to the function import table and constants according to the constant import table. For example, after a usage such as resolves to the name . Similarly, after a usage such as resolves to the name . use A\B\C;``new C()``A\B\C()``use function A\B\foo;``foo()``A\B\foo
  6. For unqualified names, if no import rule applies and the name refers to a class-like symbol, the current namespace is prepended. For example inside namespace resolves to name . new C()``A\B``A\B\C
  7. For unqualified names, if no import rule applies and the name refers to a function or constant and the code is outside the global namespace, the name is resolved at runtime. Assuming the code is in namespace , here is how a call to function is resolved: A\B``foo() 1. It looks for a function from the current namespace: . A\B\foo() 2. It tries to find and call the function . globalfoo()

Voorbeeld: 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"
            // then calls global function "foo"

\foo();     // calls function "foo" defined in global scope

my\foo();   // calls function "foo" defined in namespace "A\my"

F();        // first tries to call "F" defined in namespace "A"
            // then calls global function "F"

// class references

new B();    // creates object of class "B" defined in namespace "A"
            // if not found, it tries to autoload class "A\B"

new D();    // using import rules, creates object of class "D" defined in namespace "B"
            // if not found, it tries to autoload class "B\D"

new F();    // using import rules, creates object of class "E" defined in namespace "C"
            // if not found, it tries to autoload class "C\E"

new \B();   // creates object of class "B" defined in global scope
            // if not found, it tries to autoload class "B"

new \D();   // creates object of class "D" defined in global scope
            // if not found, it tries to autoload class "D"

new \F();   // creates object of class "F" defined in global scope
            // if not found, it tries to autoload class "F"

// static methods/namespace functions from another namespace

B\foo();    // calls function "foo" from namespace "A\B"

B::foo();   // calls method "foo" of class "B" defined in namespace "A"
            // if class "A\B" not found, it tries to autoload class "A\B"

D::foo();   // using import rules, calls method "foo" of class "D" defined in namespace "B"
            // if class "B\D" not found, it tries to autoload class "B\D"

\B\foo();   // calls function "foo" from namespace "B"

\B::foo();  // calls method "foo" of class "B" from global scope
            // if class "B" not found, it tries to autoload class "B"

// static methods/namespace functions of current namespace

A\B::foo();   // calls method "foo" of class "B" from namespace "A\A"
              // if class "A\A\B" not found, it tries to autoload class "A\A\B"

\A\B::foo();  // calls method "foo" of class "B" from namespace "A"
              // if class "A\B" not found, it tries to autoload class "A\B"
?>

Documentatie