<!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>parent</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="keyword.paamayim-nekudotayim.html">Scope Resolution Operator (::)</a></div> <div class="next" style="text-align: right; float: right;"><a href="oop4.serialization.html">Serializing objects - objects in sessions</a></div> <div class="up"><a href="oop4.html">Classes and Objects (PHP 4)</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="keyword.parent" class="sect1"> <h2 class="title"><em>parent</em></h2> <p class="para"> You may find yourself writing code that refers to variables and functions in base classes. This is particularly true if your derived class is a refinement or specialisation of code in your base class. </p> <p class="para"> Instead of using the literal name of the base class in your code, you should be using the special name <em>parent</em>, which refers to the name of your base class as given in the <em>extends</em> declaration of your class. By doing this, you avoid using the name of your base class in more than one place. Should your inheritance tree change during implementation, the change is easily made by simply changing the <em>extends</em> declaration of your class. </p> <div class="informalexample"> <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">A </span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">example</span><span style="color: #007700">() {<br /> echo </span><span style="color: #DD0000">"I am A::example() and provide basic functionality.<br />\n"</span><span style="color: #007700">;<br /> }<br />}<br /><br />class </span><span style="color: #0000BB">B </span><span style="color: #007700">extends </span><span style="color: #0000BB">A </span><span style="color: #007700">{<br /> function </span><span style="color: #0000BB">example</span><span style="color: #007700">() {<br /> echo </span><span style="color: #DD0000">"I am B::example() and provide additional functionality.<br />\n"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">parent</span><span style="color: #007700">::</span><span style="color: #0000BB">example</span><span style="color: #007700">();<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= new </span><span style="color: #0000BB">B</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// This will call B::example(), which will in turn call A::example().<br /></span><span style="color: #0000BB">$b</span><span style="color: #007700">-></span><span style="color: #0000BB">example</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="keyword.paamayim-nekudotayim.html">Scope Resolution Operator (::)</a></div> <div class="next" style="text-align: right; float: right;"><a href="oop4.serialization.html">Serializing objects - objects in sessions</a></div> <div class="up"><a href="oop4.html">Classes and Objects (PHP 4)</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>