<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.namespaces.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.namespaces.rules.php',
    1 => 'Name resolution rules',
    2 => 'Name resolution rules',
  ),
  'up' => 
  array (
    0 => 'language.namespaces.php',
    1 => 'Namespaces',
  ),
  'prev' => 
  array (
    0 => 'language.namespaces.fallback.php',
    1 => 'Fallback to global space',
  ),
  'next' => 
  array (
    0 => 'language.namespaces.faq.php',
    1 => 'FAQ',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/namespaces.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.namespaces.rules" class="sect1">
  <h2 class="title">Name resolution rules</h2>
  
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</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 <code class="literal">Foo</code>
      </p>
     </dd>
    
    
     <dt>Qualified name</dt>
     <dd>
      <p class="para">
       This is an identifier with a namespace separator, such as <code class="literal">Foo\Bar</code>
      </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 <code class="literal">\Foo\Bar</code>. The namespace
       <code class="literal">\Foo</code> is also a fully qualified name.
      </p>
     </dd>
    
    
     <dt>Relative name</dt>
     <dd>
      <p class="para">
       This is an identifier starting with <code class="literal">namespace</code>, such as
       <code class="literal">namespace\Foo\Bar</code>.
      </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 <code class="literal">\A\B</code> resolves to <code class="literal">A\B</code>.
     </span>
    </li>
    <li class="listitem">
     <span class="simpara">
      Relative names always resolve to the name with <code class="literal">namespace</code> replaced by
      the current namespace. If the name occurs in the global namespace, the
      <code class="literal">namespace\</code> prefix is stripped. For example <code class="literal">namespace\A</code>
      inside namespace <code class="literal">X\Y</code> resolves to <code class="literal">X\Y\A</code>. The same name
      inside the global namespace resolves to <code class="literal">A</code>.
     </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 <code class="literal">A\B\C</code> is
      imported as <code class="literal">C</code>, the name <code class="literal">C\D\E</code> is translated to
      <code class="literal">A\B\C\D\E</code>.
     </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 <code class="literal">C\D\E</code> inside namespace <code class="literal">A\B</code>,
      resolves to <code class="literal">A\B\C\D\E</code>.
     </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
      <code class="literal">use A\B\C;</code> a usage such as <code class="literal">new C()</code> resolves to the name
      <code class="literal">A\B\C()</code>. Similarly, after <code class="literal">use function A\B\foo;</code> a usage
      such as <code class="literal">foo()</code> resolves to the name <code class="literal">A\B\foo</code>.
     </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 <code class="literal">new C()</code> inside namespace
      <code class="literal">A\B</code> resolves to name <code class="literal">A\B\C</code>.
     </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 <code class="literal">A\B</code>, here is how a call to function
      <code class="literal">foo()</code> is resolved:
     </span>
      <ol type="1">
       <li class="listitem">
        <span class="simpara">
         It looks for a function from the current namespace:
         <code class="literal">A\B\foo()</code>.
        </span>
       </li>
       <li class="listitem">
        <span class="simpara">
         It tries to find and call the <em>global</em> function
         <code class="literal">foo()</code>.
        </span>
       </li>
      </ol>
    </li>
   </ol>
  </p>
  <div class="example" id="example-1">
   <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">&lt;?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\D</span><span style="color: #007700">, </span><span style="color: #0000BB">C\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: #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\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\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: #0000BB">\B\foo</span><span style="color: #007700">();   </span><span style="color: #FF8000">// calls function "foo" from namespace "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" 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\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: #0000BB">\A\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">?&gt;</span></span></code></div>
   </div>

  </div>
 </div><?php manual_footer($setup); ?>