language.namespaces.definition.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 103 行

HTML
103
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Namespace definition</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.namespaces.using.html">Using 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.definition" class="sect1">  <h2 class="title">Namespace definition</h2>  <p class="para">   The namespace is declared using <i>namespace</i>   keyword, which should be at the very beginning of the file. Example:   <div class="example">    <p><b>Example #1 Defining namespace</b></p>    <div class="example-contents">     <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;namespace&nbsp;MyProject</span><span style="color: #007700">::</span><span style="color: #0000BB">DB</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;</span><span style="color: #0000BB">CONNECT_OK&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;class&nbsp;</span><span style="color: #0000BB">Connection&nbsp;</span><span style="color: #007700">{&nbsp;</span><span style="color: #FF8000">/*&nbsp;...&nbsp;*/&nbsp;</span><span style="color: #007700">}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">connect</span><span style="color: #007700">()&nbsp;{&nbsp;</span><span style="color: #FF8000">/*&nbsp;...&nbsp;*/&nbsp;&nbsp;</span><span style="color: #007700">}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>   Same namespace name can be used in multiple files.  </p>    <p class="para">  Namespace can contain class, constant and function definitions, but no free code.     </p>  <p class="para">   Namespace definition does the following:   <ul class="itemizedlist">    <li class="listitem">     <span class="simpara">      Inside namespace, all class, function and constant names in definitions are      automatically prefixed with namespace name. The class name is always the      full name, i.e. in the example above the class is called      <b class="classname">MyProject::DB::Connection</b>.     </span>    </li>    <li class="listitem">    <span class="simpara">     Constant definitions create constant which is composed of namespace name and constant name.     Like class constants, namespace constant can only contains static values.    </span>    </li>    <li class="listitem">     <p class="para">      Unqualified class name (i.e., name not containing <i>::</i>)       is resolved at runtime following this procedure:      <ol class="orderedlist">       <li class="listitem">        <span class="simpara">         Class is looked up inside the current namespace (i.e. prefixing the         name with the current namespace name) without attempting to         <a href="language.oop5.autoload.html" class="link">autoload</a>.        </span>       </li>       <li class="listitem">        <span class="simpara">         Class is looked up inside the global namespace without attempting to         autoload.        </span>       </li>       <li class="listitem">        <span class="simpara">         Autoloading for name in current namespace is attempted.        </span>       </li>       <li class="listitem">        <span class="simpara">If previous failed, lookup fails.</span>       </li>      </ol>     </p>    </li>    <li class="listitem">     <p class="para">      Unqualified function name (i.e., name not containing      <i>::</i>) is looked up at runtime first in the current namespace       and then in the global space.     </p>    </li>    <li class="listitem">    <p class="para">     Unqualified constant names are looked up first at current namespace and then      among globally defined constants.    </p>    </li>   </ul>  See also the full <a href="language.namespaces.rules.html" class="link">name resolution rules</a>.  </p>  </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.namespaces.using.html">Using 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>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?