internals2.counter.examples.objective.html

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

HTML
44
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Objective interface</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="internals2.counter.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="internals2.counter.counter-class.html">Counter</a></div> <div class="up"><a href="internals2.counter.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="internals2.counter.examples.objective" class="section">    <h2 class="title">Objective interface</h2>        <p class="simpara">     The objective interface provides an object-oriented way to access the     extended interfaces. The following example shows how the above one would be     implemented using the objective interface. The output of this example is     exactly the same, except that instead of printing &quot;Not a valid counter!&quot;,     this will instead issue a PHP warning that the variable     <i>$counter_three</i> is not an object. This example shows that     it is possible to subclass the <b class="classname">Counter</b> class defined     by the extension, as well as that the counter&#039;s value is maintained using     an instance variable rather than method access.    </p>        <div class="example" id="internals2.counter.examples.objective.ex" name="internals2.counter.examples.objective.ex">     <p><b>Example #1 &quot;counter&quot;&#039;s objective interface</b></p>          <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">MyCounter&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Counter<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">printCounterInfo</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Counter's&nbsp;name&nbsp;is&nbsp;'%s'&nbsp;and&nbsp;is%s&nbsp;persistent.&nbsp;Its&nbsp;current&nbsp;value&nbsp;is&nbsp;%d.\n"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMeta</span><span style="color: #007700">(</span><span style="color: #0000BB">COUNTER_META_NAME</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMeta</span><span style="color: #007700">(</span><span style="color: #0000BB">COUNTER_META_IS_PERSISTENT</span><span style="color: #007700">)&nbsp;?&nbsp;</span><span style="color: #DD0000">''&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">'&nbsp;not'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">Counter</span><span style="color: #007700">::</span><span style="color: #0000BB">setCounterClass</span><span style="color: #007700">(</span><span style="color: #DD0000">"MyCounter"</span><span style="color: #007700">);<br />if&nbsp;((</span><span style="color: #0000BB">$counter_one&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Counter</span><span style="color: #007700">::</span><span style="color: #0000BB">getNamed</span><span style="color: #007700">(</span><span style="color: #DD0000">"one"</span><span style="color: #007700">))&nbsp;===&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$counter_one&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Counter</span><span style="color: #007700">(</span><span style="color: #DD0000">"one"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">COUNTER_FLAG_PERSIST</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">$counter_one</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bumpValue</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;we&nbsp;aren't&nbsp;allowed&nbsp;to&nbsp;"set"&nbsp;the&nbsp;value&nbsp;directly<br /></span><span style="color: #0000BB">$counter_two&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Counter</span><span style="color: #007700">(</span><span style="color: #DD0000">"two"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$counter_three&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Counter</span><span style="color: #007700">::</span><span style="color: #0000BB">getNamed</span><span style="color: #007700">(</span><span style="color: #DD0000">"three"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$counter_four&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Counter</span><span style="color: #007700">(</span><span style="color: #DD0000">"four"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">COUNTER_FLAG_PERSIST&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">COUNTER_FLAG_SAVE&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">COUNTER_FLAG_NO_OVERWRITE</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$counter_four</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bumpValue</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$counter_one</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">printCounterInfo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$counter_two</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">printCounterInfo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$counter_three</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">printCounterInfo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$counter_four</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">printCounterInfo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>     </div>    </div>   </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="internals2.counter.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="internals2.counter.counter-class.html">Counter</a></div> <div class="up"><a href="internals2.counter.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

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