<!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>Object Inheritance</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.visibility.html">Visibility</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.paamayim-nekudotayim.html">Scope Resolution Operator (::)</a></div> <div class="up"><a href="language.oop5.html">Classes and Objects</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="language.oop5.inheritance" class="sect1"> <h2 class="title">Object Inheritance</h2> <p class="para"> Inheritance is a well-established programming principle, and PHP makes use of this principle in its object model. This principle will affect the way many classes and objects relate to one another. </p> <p class="para"> For example, when you extend a class, the subclass inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality. </p> <p class="para"> This is useful for defining and abstracting functionality, and permits the implementation of additional functionality in similar objects without the need to reimplement all of the shared functionality. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Unless autoloading is used, then classes must be defined before they are used. If a class extends another, then the parent class must be declared before the child class structure. This rule applies to classes that inherit other classes and interfaces. </p> </p></blockquote> <div class="sect2" id="language.oop5.inheritance.examples"> <div class="example" id="language.oop5.inheritance.examples.ex1"> <p><strong>Example #1 Inheritance Example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">class </span><span style="color: #0000BB">foo<br /></span><span style="color: #007700">{<br /> public function </span><span style="color: #0000BB">printItem</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">)<br /> {<br /> echo </span><span style="color: #DD0000">'Foo: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$string </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /> }<br /> <br /> public function </span><span style="color: #0000BB">printPHP</span><span style="color: #007700">()<br /> {<br /> echo </span><span style="color: #DD0000">'PHP is great.' </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /> }<br />}<br /><br />class </span><span style="color: #0000BB">bar </span><span style="color: #007700">extends </span><span style="color: #0000BB">foo<br /></span><span style="color: #007700">{<br /> public function </span><span style="color: #0000BB">printItem</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">)<br /> {<br /> echo </span><span style="color: #DD0000">'Bar: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$string </span><span style="color: #007700">. </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$foo </span><span style="color: #007700">= new </span><span style="color: #0000BB">foo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$bar </span><span style="color: #007700">= new </span><span style="color: #0000BB">bar</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$foo</span><span style="color: #007700">-></span><span style="color: #0000BB">printItem</span><span style="color: #007700">(</span><span style="color: #DD0000">'baz'</span><span style="color: #007700">); </span><span style="color: #FF8000">// Output: 'Foo: baz'<br /></span><span style="color: #0000BB">$foo</span><span style="color: #007700">-></span><span style="color: #0000BB">printPHP</span><span style="color: #007700">(); </span><span style="color: #FF8000">// Output: 'PHP is great' <br /></span><span style="color: #0000BB">$bar</span><span style="color: #007700">-></span><span style="color: #0000BB">printItem</span><span style="color: #007700">(</span><span style="color: #DD0000">'baz'</span><span style="color: #007700">); </span><span style="color: #FF8000">// Output: 'Bar: baz'<br /></span><span style="color: #0000BB">$bar</span><span style="color: #007700">-></span><span style="color: #0000BB">printPHP</span><span style="color: #007700">(); </span><span style="color: #FF8000">// Output: 'PHP is great'<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.oop5.visibility.html">Visibility</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.oop5.paamayim-nekudotayim.html">Scope Resolution Operator (::)</a></div> <div class="up"><a href="language.oop5.html">Classes and Objects</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>