keyword.paamayim-nekudotayim.html

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

HTML
77
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Scope Resolution Operator (::)</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.oop.constructor.html">Constructors</a></div> <div class="next" style="text-align: right; float: right;"><a href="keyword.parent.html">parent</a></div> <div class="up"><a href="language.oop.html">Classes and Objects (PHP 4)</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="keyword.paamayim-nekudotayim" class="sect1">   <h2 class="title">Scope Resolution Operator (<i>::</i>)</h2>   <div class="caution"><b class="caution">Caution</b>    <p class="simpara">     The following is valid for PHP 4 and later only.    </p>   </div>   <p class="para">    Sometimes it is useful to refer to functions and variables    in base classes or to refer to functions in classes that    have not yet any instances. The :: operator is being used    for this.   </p>      <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: #007700">class&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">example</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I&nbsp;am&nbsp;the&nbsp;original&nbsp;function&nbsp;A::example().&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">B&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">A&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">example</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I&nbsp;am&nbsp;the&nbsp;redefined&nbsp;function&nbsp;B::example().&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">A</span><span style="color: #007700">::</span><span style="color: #0000BB">example</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;there&nbsp;is&nbsp;no&nbsp;object&nbsp;of&nbsp;class&nbsp;A.<br />//&nbsp;this&nbsp;will&nbsp;print<br />//&nbsp;&nbsp;&nbsp;I&nbsp;am&nbsp;the&nbsp;original&nbsp;function&nbsp;A::example().&lt;br&nbsp;/&gt;<br /></span><span style="color: #0000BB">A</span><span style="color: #007700">::</span><span style="color: #0000BB">example</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">//&nbsp;create&nbsp;an&nbsp;object&nbsp;of&nbsp;class&nbsp;B.<br /></span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">B</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;this&nbsp;will&nbsp;print&nbsp;<br />//&nbsp;&nbsp;&nbsp;I&nbsp;am&nbsp;the&nbsp;redefined&nbsp;function&nbsp;B::example().&lt;br&nbsp;/&gt;<br />//&nbsp;&nbsp;&nbsp;I&nbsp;am&nbsp;the&nbsp;original&nbsp;function&nbsp;A::example().&lt;br&nbsp;/&gt;<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">example</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>   <p class="para">    The above example calls the function example() in    class A, but there is no object of class A, so that    we cannot write $a-&gt;example() or similar. Instead we    call example() as a &#039;class function&#039;, that is, as a    function of the class itself, not any object of that    class.   </p>      <p class="para">    There are class functions, but there are no class variables.    In fact, there is no object at all at the time of the call.    Thus, a class function may not use any object variables (but    it can use local and global variables), and it may not use    <var class="varname">$this</var> at all.   </p>   <p class="para">    In the above example, class B redefines the function example().     The original definition in class A is shadowed    and no longer available, unless you are referring specifically    to the implementation of example() in class A using the     ::-operator. Write A::example() to do this (in fact, you    should be writing parent::example(), as shown in the next    section).   </p>      <p class="para">    In this context, there is a current object and it may have object    variables. Thus, when used from WITHIN an object function, you may use    <var class="varname">$this</var> and object variables.   </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop.constructor.html">Constructors</a></div> <div class="next" style="text-align: right; float: right;"><a href="keyword.parent.html">parent</a></div> <div class="up"><a href="language.oop.html">Classes and Objects (PHP 4)</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

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