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"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">example</span><span style="color: #007700">() {<br /> echo </span><span style="color: #DD0000">"I am the original function A::example().<br />\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br />class </span><span style="color: #0000BB">B </span><span style="color: #007700">extends </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">example</span><span style="color: #007700">() {<br /> echo </span><span style="color: #DD0000">"I am the redefined function B::example().<br />\n"</span><span style="color: #007700">;<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 />}<br /><br /></span><span style="color: #FF8000">// there is no object of class A.<br />// this will print<br />// I am the original function A::example().<br /><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">// create an object of class B.<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= new </span><span style="color: #0000BB">B</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// this will print <br />// I am the redefined function B::example().<br /><br />// I am the original function A::example().<br /><br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">-></span><span style="color: #0000BB">example</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></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->example() or similar. Instead we call example() as a 'class function', 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 + -
显示快捷键?