<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Name resolution rules</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.namespaces.fallback.html">Using namespaces: fallback to global function/constant</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.namespaces.faq.html">FAQ: things you need to know about namespaces</a></div> <div class="up"><a href="language.namespaces.html">Namespaces</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="language.namespaces.rules" class="sect1"> <h2 class="title">Name resolution rules</h2> <p class="verinfo">(PHP 5 >= 5.3.0, PHP 7)</p> <p class="para"> For the purposes of these resolution rules, here are some important definitions: <dl> <strong class="title">Namespace name definitions</strong> <dt> Unqualified name</dt> <dd> <p class="para"> This is an identifier without a namespace separator, such as <em>Foo</em> </p> </dd> <dt> Qualified name</dt> <dd> <p class="para"> This is an identifier with a namespace separator, such as <em>Foo\Bar</em> </p> </dd> <dt> Fully qualified name</dt> <dd> <p class="para"> This is an identifier with a namespace separator that begins with a namespace separator, such as <em>\Foo\Bar</em>. The namespace <em>\Foo</em> is also a fully qualified name. </p> </dd> <dt> Relative name</dt> <dd> <p class="para"> This is an identifier starting with <em>namespace</em>, such as <em>namespace\Foo\Bar</em>. </p> </dd> </dl> </p> <p class="para"> Names are resolved following these resolution rules: <ol type="1"> <li class="listitem"> <span class="simpara"> Fully qualified names always resolve to the name without leading namespace separator. For instance <em>\A\B</em> resolves to <em>A\B</em>. </span> </li> <li class="listitem"> <span class="simpara"> Relative names always resolve to the name with <em>namespace</em> replaced by the current namespace. If the name occurs in the global namespace, the <em>namespace\</em> prefix is stripped. For example <em>namespace\A</em> inside namespace <em>X\Y</em> resolves to <em>X\Y\A</em>. The same name inside the global namespace resolves to <em>A</em>. </span> </li> <li class="listitem"> <span class="simpara"> For qualified names the first segment of the name is translated according to the current class/namespace import table. For example, if the namespace <em>A\B\C</em> is imported as <em>C</em>, the name <em>C\D\E</em> is translated to <em>A\B\C\D\E</em>. </span> </li> <li class="listitem"> <span class="simpara"> For qualified names, if no import rule applies, the current namespace is prepended to the name. For example, the name <em>C\D\E</em> inside namespace <em>A\B</em>, resolves to <em>A\B\C\D\E</em>. </span> </li> <li class="listitem"> <span class="simpara"> 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 <em>use A\B\C;</em> a usage such as <em>new C()</em> resolves to the name <em>A\B\C()</em>. Similarly, after <em>use function A\B\fn;</em> a usage such as <em>fn()</em> resolves to the name <em>A\B\fn</em>. </span> </li> <li class="listitem"> <span class="simpara"> For unqualified names, if no import rule applies and the name refers to a class-like symbol, the current namespace is prepended. For example <em>new C()</em> inside namespace <em>A\B</em> resolves to name <em>A\B\C</em>. </span> </li> <li class="listitem"> <span class="simpara"> 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 <em>A\B</em>, here is how a call to function <em>foo()</em> is resolved: </span> <ol type="1"> <li class="listitem"> <span class="simpara"> It looks for a function from the current namespace: <em>A\B\foo()</em>. </span> </li> <li class="listitem"> <span class="simpara"> It tries to find and call the <em class="emphasis">global</em> function <em>foo()</em>. </span> </li> </ol> </li> </ol> </p> <div class="example" id="example-266"> <p><strong>Example #1 Name resolutions illustrated</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">namespace </span><span style="color: #0000BB">A</span><span style="color: #007700">;<br />use </span><span style="color: #0000BB">B</span><span style="color: #007700">\</span><span style="color: #0000BB">D</span><span style="color: #007700">, </span><span style="color: #0000BB">C</span><span style="color: #007700">\</span><span style="color: #0000BB">E </span><span style="color: #007700">as </span><span style="color: #0000BB">F</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// function calls<br /><br /></span><span style="color: #0000BB">foo</span><span style="color: #007700">(); </span><span style="color: #FF8000">// first tries to call "foo" defined in namespace "A"<br /> // then calls global function "foo"<br /><br /></span><span style="color: #007700">\</span><span style="color: #0000BB">foo</span><span style="color: #007700">(); </span><span style="color: #FF8000">// calls function "foo" defined in global scope<br /><br /></span><span style="color: #0000BB">my</span><span style="color: #007700">\</span><span style="color: #0000BB">foo</span><span style="color: #007700">(); </span><span style="color: #FF8000">// calls function "foo" defined in namespace "A\my"<br /><br /></span><span style="color: #0000BB">F</span><span style="color: #007700">(); </span><span style="color: #FF8000">// first tries to call "F" defined in namespace "A"<br /> // then calls global function "F"<br /><br />// class references<br /><br /></span><span style="color: #007700">new </span><span style="color: #0000BB">B</span><span style="color: #007700">(); </span><span style="color: #FF8000">// creates object of class "B" defined in namespace "A"<br /> // if not found, it tries to autoload class "A\B"<br /><br /></span><span style="color: #007700">new </span><span style="color: #0000BB">D</span><span style="color: #007700">(); </span><span style="color: #FF8000">// using import rules, creates object of class "D" defined in namespace "B"<br /> // if not found, it tries to autoload class "B\D"<br /><br /></span><span style="color: #007700">new </span><span style="color: #0000BB">F</span><span style="color: #007700">(); </span><span style="color: #FF8000">// using import rules, creates object of class "E" defined in namespace "C"<br /> // if not found, it tries to autoload class "C\E"<br /><br /></span><span style="color: #007700">new \</span><span style="color: #0000BB">B</span><span style="color: #007700">(); </span><span style="color: #FF8000">// creates object of class "B" defined in global scope<br /> // if not found, it tries to autoload class "B"<br /><br /></span><span style="color: #007700">new \</span><span style="color: #0000BB">D</span><span style="color: #007700">(); </span><span style="color: #FF8000">// creates object of class "D" defined in global scope<br /> // if not found, it tries to autoload class "D"<br /><br /></span><span style="color: #007700">new \</span><span style="color: #0000BB">F</span><span style="color: #007700">(); </span><span style="color: #FF8000">// creates object of class "F" defined in global scope<br /> // if not found, it tries to autoload class "F"<br /><br />// static methods/namespace functions from another namespace<br /><br /></span><span style="color: #0000BB">B</span><span style="color: #007700">\</span><span style="color: #0000BB">foo</span><span style="color: #007700">(); </span><span style="color: #FF8000">// calls function "foo" from namespace "A\B"<br /><br /></span><span style="color: #0000BB">B</span><span style="color: #007700">::</span><span style="color: #0000BB">foo</span><span style="color: #007700">(); </span><span style="color: #FF8000">// calls method "foo" of class "B" defined in namespace "A"<br /> // if class "A\B" not found, it tries to autoload class "A\B"<br /><br /></span><span style="color: #0000BB">D</span><span style="color: #007700">::</span><span style="color: #0000BB">foo</span><span style="color: #007700">(); </span><span style="color: #FF8000">// using import rules, calls method "foo" of class "D" defined in namespace "B"<br /> // if class "B\D" not found, it tries to autoload class "B\D"<br /><br /></span><span style="color: #007700">\</span><span style="color: #0000BB">B</span><span style="color: #007700">\</span><span style="color: #0000BB">foo</span><span style="color: #007700">(); </span><span style="color: #FF8000">// calls function "foo" from namespace "B"<br /><br /></span><span style="color: #007700">\</span><span style="color: #0000BB">B</span><span style="color: #007700">::</span><span style="color: #0000BB">foo</span><span style="color: #007700">(); </span><span style="color: #FF8000">// calls method "foo" of class "B" from global scope<br /> // if class "B" not found, it tries to autoload class "B"<br /><br />// static methods/namespace functions of current namespace<br /><br /></span><span style="color: #0000BB">A</span><span style="color: #007700">\</span><span style="color: #0000BB">B</span><span style="color: #007700">::</span><span style="color: #0000BB">foo</span><span style="color: #007700">(); </span><span style="color: #FF8000">// calls method "foo" of class "B" from namespace "A\A"<br /> // if class "A\A\B" not found, it tries to autoload class "A\A\B"<br /><br /></span><span style="color: #007700">\</span><span style="color: #0000BB">A</span><span style="color: #007700">\</span><span style="color: #0000BB">B</span><span style="color: #007700">::</span><span style="color: #0000BB">foo</span><span style="color: #007700">(); </span><span style="color: #FF8000">// calls method "foo" of class "B" from namespace "A"<br /> // if class "A\B" not found, it tries to autoload class "A\B"<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.namespaces.fallback.html">Using namespaces: fallback to global function/constant</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.namespaces.faq.html">FAQ: things you need to know about namespaces</a></div> <div class="up"><a href="language.namespaces.html">Namespaces</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>