Last time we looked at the scope possibilities for class a properties. In part 3 of our OOP PHP blog series, we will be looking at inheritance. Inheritance is an approach in PHP programming that allows for the extension of a class in order to add more functionality to an existing class. There is also the option to override existing functionality and replace portions of the class if so desired.
In our example code for creating HTML tags we are going to extend our basic class that only has three methods, we will be adding three more methods, we will be using the existing constructor method from the parent class (html), and we will override the pageFooter method.
In order to inherit a class you need to use the “extends” keyword. Note that a single class can only extend one other class at a time, there is no limit to the inheritance chain, practicality however suggests no more than five inheritances in a chain, otherwise the length of the chain becomes hard to navigate and manage. Also note that all of the public and protected methods (this will be covered in part four of our blog series) are inherited in the subclass and unless they are overridden they retain their original functionality as if the code for those methods were actually in the subclass.
So we are first going to extend the HTML class to a new class called moreHTML. We can do this in the same class code file if we want or we can do it in its own file. Just make sure the “require” statements are in the right order; here's our new code (our original html.php.inc file has not changed so I am not re-listing it here):
// filename: more_html.php.inc
class moreHTML extends html {
private $tag;
private $textString ;
function pageFooter() {
$textString = '\n<hr/>';
$textString .= html::pageFooter();
return $textString;
}
function Anchor($url, $link, $name="", $class="", $id="", $title="", $style="", $onTypesActions="") {
$this->tag = '<a ' ;
if ($url) { $this->tag .= 'href="' . $url . '" ' ; } else { $this->tag .= 'href="" ' ; }
if ($name) $this->tag .= 'name="' . $name . '" ' ;
if ($class) $this->tag .= 'class="' . $class . '" ' ;
if ($id) $this->tag .= 'id="' . $id . '" ' ;
if ($title) $this->tag .= 'title="' . $title . '" ' ;
if ($style) $this->tag .= 'style="' . $style . '" ' ;
if (!Empty($onTypesActions)){
foreach($onTypesActions AS $type => $actoin) {
$this->tag .= $type . '="' . $actoin . '" ' ;
}
}
$this->tag .= ">" ;
$this->tag .= $link ;
$this->tag .= "</a>" ;
return $this->tag ;
}
function Spacer($spaces = 1) {
$this->tag = "";
for ($i=1 ; $i <= $spaces ; $i++) {
$this->tag .= " " ;
}
return $this->tag;
}
function NewLine($number = 1) {
$this->tag = '';
for ($i=1 ; $i <= $number ; $i++) {
$this->tag .= "\n<br/>" ;
}
return $this->tag;
}
} // end moreHTML class definition
Notice that our newly extended class does not have a __constructor method at all, it merely inherits that from the parent; this is equally true for the image method. Also notice that we are over-riding the pageFooter method by adding in an <hr> tag to signify the bottom of the page and then we are calling the parent method with the :: notation, to leverage that existing code. Our code to use these classes would look something like this:
require_once("html.php.inc");
require_once("more_html.php.inc");
$myHTML = NEW moreHTML("Testing page build 3");
$output = $myHTML->Anchor("http://php.osscube.com", $myHTML->image("images/clock.jpg")) ;
echo $myHTML->NewLine() ;
echo "the current value of label: " . $myHTML->Spacer(3) . $myHTML->label . $myHTML->NewLine(2) . "\n";
$output .= $myHTML->NewLine(2) ;
$output .= $myHTML->pageFooter();
echo $output;
Notice here that I am using a method call as the 2nd parameter to the anchor method; this is a neat technique that can be used when attempting to use methods that are inter-related. As well, I have the choice (depending on how the methods are actually written) to either build the $output variable and then echo that out when I want, or to echo method calls directly to the browser. See too that I am using our two new methods in the inherited class called NewLine and Spacer. When the above code is executed you will see the following content on a browser:

And you should see the following in your revealed source code:
<HTML>
<HEAD>
<TITLE>Testing page build 3</TITlE>
</HEAD>
<BODY>
<br/>the current value of label: this is my label
<br/>
<br/>
<a href="http://php.osscube.com" ><img src="images/clock.jpg" /></a>
<br/>
<br/>
<hr />
</body>
</html>
In our fourth blog segment we will be looking at the scope attributes that can be added to methods, this will be similar in concept to scope attributes that can be added to properties of a class.


