📄 language.oop5.late-static-bindings.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Late Static Bindings</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.oop5.typehinting.html">Type Hinting</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.references.html">Objects and references</a></div> <div class="up"><a href="language.oop5.html">Classes and Objects (PHP 5)</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="language.oop5.late-static-bindings" class="sect1"> <h2 class="title">Late Static Bindings</h2> <p class="para"> As of PHP 5.3.0, PHP implements a feature called late static bindings which can be used to reference the called class in a context of static inheritance. </p> <p class="para"> This feature was named "late static bindings" with an internal perspective in mind. "Late binding" comes from the fact that <i>static::</i> will no longer be resolved using the class where the method is defined but it will rather be computed using runtime information. It was also called a "static binding" as it can be used for (but is not limited to) static method calls. </p> <div id="language.oop5.late-static-bindings.self" class="sect2"> <h3 class="title">Limitations of <i>self::</i></h3> <p class="para"> Static references to the current class like <i>self::</i> or <i>__CLASS__</i> are resolved using the class in which the function belongs, as in where it was defined: </p> <div class="example"> <p><b>Example #1 <i>self::</i> usage</b></p> <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 /> public static function </span><span style="color: #0000BB">who</span><span style="color: #007700">() {<br /> echo </span><span style="color: #0000BB">__CLASS__</span><span style="color: #007700">;<br /> }<br /> public static function </span><span style="color: #0000BB">test</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">self</span><span style="color: #007700">::</span><span style="color: #0000BB">who</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 /> public static function </span><span style="color: #0000BB">who</span><span style="color: #007700">() {<br /> echo </span><span style="color: #0000BB">__CLASS__</span><span style="color: #007700">;<br /> } <br />} <br /><br /></span><span style="color: #0000BB">B</span><span style="color: #007700">::</span><span style="color: #0000BB">test</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>A</pre></div> </pre></div> </div> </div> <div id="language.oop5.late-static-bindings.usage" class="sect2"> <h3 class="title">Late Static Bindings' usage</h3> <p class="para"> Late static bindings tries to solve that limitation by introducing a keyword that references the class that was initially called at runtime. Basically, a keyword that would allow you to reference <i>B</i> from <i>test()</i> in the previous example. It was decided not to introduce a new keyword but rather use <i>static</i> that was already reserved. </p> <div class="example"> <p><b>Example #2 <i>static::</i> simple usage</b></p> <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 /> public static function </span><span style="color: #0000BB">who</span><span style="color: #007700">() {<br /> echo </span><span style="color: #0000BB">__CLASS__</span><span style="color: #007700">;<br /> }<br /> public static function </span><span style="color: #0000BB">test</span><span style="color: #007700">() {<br /> static::</span><span style="color: #0000BB">who</span><span style="color: #007700">(); </span><span style="color: #FF8000">// Here comes Late Static Bindings <br /> </span><span style="color: #007700">} <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 /> public static function </span><span style="color: #0000BB">who</span><span style="color: #007700">() {<br /> echo </span><span style="color: #0000BB">__CLASS__</span><span style="color: #007700">;<br /> } <br />} <br /><br /></span><span style="color: #0000BB">B</span><span style="color: #007700">::</span><span style="color: #0000BB">test</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents"><pre><div class="cdata"><pre>B</pre></div> </pre></div> </div>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -