<!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>Extending</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="reflection.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="class.reflection.html">Reflection</a></div> <div class="up"><a href="book.reflection.html">Reflection</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="reflection.extending" class="chapter"> <h1>Extending</h1> <p class="para"> If you want to create specialized versions of the built-in classes (say, for creating colorized HTML when being exported, having easy-access member variables instead of methods or having utility methods), you may go ahead and extend them. </p> <div class="example" id="example-5179"> <p><strong>Example #1 Extending the built-in classes</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">/**<br /> * My Reflection_Method class<br /> */<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">My_Reflection_Method </span><span style="color: #007700">extends </span><span style="color: #0000BB">ReflectionMethod<br /></span><span style="color: #007700">{<br /> public </span><span style="color: #0000BB">$visibility </span><span style="color: #007700">= array();<br /><br /> public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$o</span><span style="color: #007700">, </span><span style="color: #0000BB">$m</span><span style="color: #007700">)<br /> {<br /> </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$o</span><span style="color: #007700">, </span><span style="color: #0000BB">$m</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">visibility </span><span style="color: #007700">= </span><span style="color: #0000BB">Reflection</span><span style="color: #007700">::</span><span style="color: #0000BB">getModifierNames</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">getModifiers</span><span style="color: #007700">());<br /> }<br />}<br /><br /></span><span style="color: #FF8000">/**<br /> * Demo class #1<br /> *<br /> */<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">T </span><span style="color: #007700">{<br /> protected function </span><span style="color: #0000BB">x</span><span style="color: #007700">() {}<br />}<br /><br /></span><span style="color: #FF8000">/**<br /> * Demo class #2<br /> *<br /> */<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">U </span><span style="color: #007700">extends </span><span style="color: #0000BB">T </span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">x</span><span style="color: #007700">() {}<br />}<br /><br /></span><span style="color: #FF8000">// Print out information<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(new </span><span style="color: #0000BB">My_Reflection_Method</span><span style="color: #007700">(</span><span style="color: #DD0000">'U'</span><span style="color: #007700">, </span><span style="color: #DD0000">'x'</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 something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> object(My_Reflection_Method)#1 (3) { ["visibility"]=> array(1) { [0]=> string(6) "public" } ["name"]=> string(1) "x" ["class"]=> string(1) "U" } </pre></div> </div> </div> <div class="caution"><strong class="caution">Caution</strong> <p class="para"> If you're overwriting the constructor, remember to call the parent's constructor before any code you insert. Failing to do so will result in the following: <em>Fatal error: Internal error: Failed to retrieve the reflection object</em> </p> </div> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="reflection.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="class.reflection.html">Reflection</a></div> <div class="up"><a href="book.reflection.html">Reflection</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>