📄 language.namespaces.rules.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Name resolution rules</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.namespaces.html">Namespaces</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.exceptions.html">Exceptions</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="para"> Names are resolved following these resolution rules: <ol class="orderedlist"> <li class="listitem"> <span class="simpara"> All qualified names are translated during compilation according to current import rules. In example, if the namespace A::B::C is imported, a call to <code class="code">C::D::e()</code> is translated to <code class="code">A::B::C::D::e()</code>. </span> </li> <li class="listitem"> <span class="simpara"> Unqualified class names are translated during compilation according to current import rules (full name substituted for short imported name). In example, if the namespace <i>A::B::C</i> is imported, <code class="code">new C()</code> is translated to <code class="code">new A::B::C()</code>. </span> </li> <li class="listitem"> <span class="simpara"> Inside namespace, calls to unqualified functions that are defined in the current namespace (and are known at the time the call is parsed) are interpreted as calls to these namespace functions, at compile time. </span> </li> <li class="listitem"> <span class="simpara"> Inside namespace (say A::B), calls to unqualified functions that are not defined in current namespace are resolved at run-time. Here is how a call to function <i>foo()</i> is resolved: </span> <ol class="orderedlist"> <li class="listitem"> <span class="simpara"> It looks for a function from the current namespace: <i>A::B::foo()</i>. </span> </li> <li class="listitem"> <span class="simpara"> It tries to find and call the <em class="emphasis">internal</em> function <i>foo()</i>. </span> </li> </ol> <span class="simpara"> To call a user defined function in the global namespace, <i>::foo()</i> has to be used. </span> </li> <li class="listitem"> <span class="simpara"> Inside namespace (say <i>A::B</i>), calls to unqualified class names are resolved at run-time. Here is how a call to <code class="code">new C()</code> is resolved: </span> <ol class="orderedlist"> <li class="listitem"> <span class="simpara"> It looks for a class from the current namespace: <i>A::B::C</i>. </span> </li> <li class="listitem"> <span class="simpara"> It tries to find and call the <em class="emphasis">internal</em> class <i>C</i>. </span> </li> <li class="listitem"> <span class="simpara"> It attemts to autoload <i>A::B::C</i>. </span> </li> </ol> <span class="simpara"> To reference a user defined class in the global namespace, <code class="code">new ::C()</code> has to be used. </span> </li> <li class="listitem"> <span class="simpara"> Calls to qualified functions are resolved at run-time. Here is how a call to <i>A::B::foo()</i> is resolved: </span> <ol class="orderedlist"> <li class="listitem"> <span class="simpara"> It looks for a function <i>foo()</i> in the namespace <i>A::B</i>. </span> </li> <li class="listitem"> <span class="simpara"> It looks for a class <i>A::B</i> and call its static method <i>foo()</i>. It will autoload the class if necessary. </span> </li> </ol> </li> <li class="listitem"> <span class="simpara"> Qualified class names are resolved in compile-time as class from corresponding namespace. For example, <code class="code">new A::B::C()</code> refers to class <b class="classname">C</b> from namespace <i>A::B</i>. </span> </li> </ol> </p> <div class="example"> <p><b>Example #1 Name resolutions illustrated</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />namespace A</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 internal 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 />// 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">// first tries to create object of class "B" defined in namespace "A"<br /> // then creates object of internal class "B"<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 /><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">// first tries to call function "foo" from namespace "A::B"<br /> // then calls method "foo" of internal class "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">// first tries to call function "foo" from namespace "B"<br /> // then calls method "foo" of class "B" from global scope<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">foo</span><span style="color: #007700">(); </span><span style="color: #FF8000">// first tries to call function "foo" from namespace "A::A"<br /> // then tries to call method "foo" of class "A" from namespace "A"<br /> // then tries to call function "foo" from namespace "A"<br /> // then calls method "foo" of internal class "A" <br /><br /></span><span style="color: #007700">::</span><span style="color: #0000BB">A</span><span style="color: #007700">::</span><span style="color: #0000BB">foo</span><span style="color: #007700">(); </span><span style="color: #FF8000">// first tries to call function "foo" from namespace "A"<br /> // then calls method "foo" of class "A" from global scope<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.namespaces.html">Namespaces</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.exceptions.html">Exceptions</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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -