Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 8264

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Objective interface</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="internals2.counter.examples.extended.html">Extended interface</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
     <em>$counter_three</em> is not an object. This example shows that
     it is possible to subclass the <strong class="classname">Counter</strong> 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">
     <p><strong>Example #1 &quot;counter&quot;&#039;s objective interface</strong></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 class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="internals2.counter.examples.extended.html">Extended interface</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>