Sophie

Sophie

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

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>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">&lt;?php<br /></span><span style="color: #FF8000">/**<br />&nbsp;*&nbsp;My&nbsp;Reflection_Method&nbsp;class<br />&nbsp;*/<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">My_Reflection_Method&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">ReflectionMethod<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$visibility&nbsp;</span><span style="color: #007700">=&nbsp;array();<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">(</span><span style="color: #0000BB">$o</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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">,&nbsp;</span><span style="color: #0000BB">$m</span><span style="color: #007700">);<br />&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">visibility&nbsp;</span><span style="color: #007700">=&nbsp;</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">-&gt;</span><span style="color: #0000BB">getModifiers</span><span style="color: #007700">());<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #FF8000">/**<br />&nbsp;*&nbsp;Demo&nbsp;class&nbsp;#1<br />&nbsp;*<br />&nbsp;*/<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">T&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;function&nbsp;</span><span style="color: #0000BB">x</span><span style="color: #007700">()&nbsp;{}<br />}<br /><br /></span><span style="color: #FF8000">/**<br />&nbsp;*&nbsp;Demo&nbsp;class&nbsp;#2<br />&nbsp;*<br />&nbsp;*/<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">U&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">T&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;</span><span style="color: #0000BB">x</span><span style="color: #007700">()&nbsp;{}<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;Print&nbsp;out&nbsp;information<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">My_Reflection_Method</span><span style="color: #007700">(</span><span style="color: #DD0000">'U'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'x'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</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) {
  [&quot;visibility&quot;]=&gt;
  array(1) {
    [0]=&gt;
    string(6) &quot;public&quot;
  }
  [&quot;name&quot;]=&gt;
  string(1) &quot;x&quot;
  [&quot;class&quot;]=&gt;
  string(1) &quot;U&quot;
}
</pre></div>
  </div>
 </div>
 <div class="caution"><strong class="caution">Caution</strong>
  <p class="para">
   If you&#039;re overwriting the constructor, remember to call
   the parent&#039;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>