<!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 "Not a valid counter!", 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'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 "counter"'s objective interface</strong></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">MyCounter </span><span style="color: #007700">extends </span><span style="color: #0000BB">Counter<br /></span><span style="color: #007700">{<br /> public function </span><span style="color: #0000BB">printCounterInfo</span><span style="color: #007700">() {<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Counter's name is '%s' and is%s persistent. Its current value is %d.\n"</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></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 /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></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">) ? </span><span style="color: #DD0000">'' </span><span style="color: #007700">: </span><span style="color: #DD0000">' not'</span><span style="color: #007700">,<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">value</span><span style="color: #007700">);<br /> }<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 ((</span><span style="color: #0000BB">$counter_one </span><span style="color: #007700">= </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">)) === </span><span style="color: #0000BB">NULL</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$counter_one </span><span style="color: #007700">= new </span><span style="color: #0000BB">Counter</span><span style="color: #007700">(</span><span style="color: #DD0000">"one"</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">, </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">-></span><span style="color: #0000BB">bumpValue</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">); </span><span style="color: #FF8000">// we aren't allowed to "set" the value directly<br /></span><span style="color: #0000BB">$counter_two </span><span style="color: #007700">= new </span><span style="color: #0000BB">Counter</span><span style="color: #007700">(</span><span style="color: #DD0000">"two"</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$counter_three </span><span style="color: #007700">= </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 </span><span style="color: #007700">= new </span><span style="color: #0000BB">Counter</span><span style="color: #007700">(</span><span style="color: #DD0000">"four"</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">COUNTER_FLAG_PERSIST </span><span style="color: #007700">| </span><span style="color: #0000BB">COUNTER_FLAG_SAVE </span><span style="color: #007700">| </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">-></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">-></span><span style="color: #0000BB">printCounterInfo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$counter_two</span><span style="color: #007700">-></span><span style="color: #0000BB">printCounterInfo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$counter_three</span><span style="color: #007700">-></span><span style="color: #0000BB">printCounterInfo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$counter_four</span><span style="color: #007700">-></span><span style="color: #0000BB">printCounterInfo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></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>