language.namespaces.using.html

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

HTML
82
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Using namespaces</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.definition.html">Namespace definition</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.namespaces.global.html">Global space</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.using" class="sect1">  <h2 class="title">Using namespaces</h2>  <p class="para">   Every class and function in a namespace can be referred to by the full name -   e.g.  <b class="classname">MyProject::DB::Connection</b> or   <b class="classname">MyProject::DB::connect</b> - at any time.   <div class="example">    <p><b>Example #1 Using namespaced name</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;</span><span style="color: #007700">require&nbsp;</span><span style="color: #DD0000">'MyProject/Db/Connection.php'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$x&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyProject</span><span style="color: #007700">::</span><span style="color: #0000BB">DB</span><span style="color: #007700">::</span><span style="color: #0000BB">Connection</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">MyProject</span><span style="color: #007700">::</span><span style="color: #0000BB">DB</span><span style="color: #007700">::</span><span style="color: #0000BB">connect</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   Namespaces can be imported into current context (global or namespace) using   the <i>use</i> operator. The syntax for the operator is:   <div class="informalexample">    <div class="example-contents">     <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;...&nbsp;*/<br /></span><span style="color: #007700">use&nbsp;</span><span style="color: #0000BB">Some</span><span style="color: #007700">::</span><span style="color: #0000BB">Name&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">Othername</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;simplified&nbsp;form&nbsp;of&nbsp;use:<br /></span><span style="color: #007700">use&nbsp;</span><span style="color: #0000BB">Foo</span><span style="color: #007700">::</span><span style="color: #0000BB">Bar</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;which&nbsp;is&nbsp;the&nbsp;same&nbsp;as&nbsp;:<br /></span><span style="color: #007700">use&nbsp;</span><span style="color: #0000BB">Foo</span><span style="color: #007700">::</span><span style="color: #0000BB">Bar&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">Bar</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>   The imported name works as follows: every time that the compiler encounters   the local name <i>Othername</i> (as stand-alone name or as    prefix to the longer name separated by <i>::</i>) the imported    name <i>Some::Name</i> is substituted instead.  </p>  <p class="para">   <i>use</i> can be used only in global scope, not inside    function or class. Imported names have effect from the point of import to    the end of the current file. It is recommended to put imports at the    beginning of the file to avoid confusion.  </p>  <p class="para">   <div class="example">    <p><b>Example #2 Importing and accessing 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;</span><span style="color: #007700">require&nbsp;</span><span style="color: #DD0000">'MyProject/Db/Connection.php'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;use&nbsp;</span><span style="color: #0000BB">MyProject</span><span style="color: #007700">::</span><span style="color: #0000BB">DB</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;use&nbsp;</span><span style="color: #0000BB">MyProject</span><span style="color: #007700">::</span><span style="color: #0000BB">DB</span><span style="color: #007700">::</span><span style="color: #0000BB">Connection&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">DbConnection</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$x&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MyProject</span><span style="color: #007700">::</span><span style="color: #0000BB">DB</span><span style="color: #007700">::</span><span style="color: #0000BB">Connection</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$y&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DB</span><span style="color: #007700">::</span><span style="color: #0000BB">connection</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$z&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">DbConnection</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">DB</span><span style="color: #007700">::</span><span style="color: #0000BB">connect</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   <blockquote><p><b class="note">Note</b>:     <span class="simpara">     The import operation is compile-time only, all local names are converted to     their full equivalents by the compiler. Note that it won&#039;t translate names     in strings, so callbacks can&#039;t rely on import rules.    </span>   </p></blockquote>  </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.namespaces.definition.html">Namespace definition</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.namespaces.global.html">Global space</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 + -
显示快捷键?