In object-oriented programming, inheritance means that a new class can be derived from an existing class. The concept is easily implemented in object-oriented languages such as Java or C#. Inheritance can also be implemented in php. The method is quite straightforward: Use the extends keyword. The following example is a simple implementation of the concept:
In the following example, I have 3 files: Person.class, Student.class, and TestStudent.php. The Person.class has the following attributes: Last name, First name, and Middle name. It has one display method that returns an html code that displays the attributes of the person. The Student.class inherits the Person.class and also has its own attributes: Id, Year/Level, and Course. The TestStudent.php tests the Student class by asking for inputs of all six attributes using only the Student class and displays all of them in tabular form.
Person.class
<?php
class Person{
?>
Student.class
<?php
include_once("Person.class");
Class Student extends Person{
}
?>
TestStudent.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Sans Titre</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="generator" content="HAPedit 3.1">
</head>
<body bgcolor="#FFFFFF">
<form name="myform" method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>" >
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="FirstName" /> </td>
</tr>
<tr>
<td>Middle Name:</td>
<td><input type="text" name="MiddleName" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="LastName" /> </td>
</tr>
<tr>
<td>Id:</td>
<td><input type="text" name="Id" /></td>
</tr>
<tr>
<td>Year:</td>
<td><input type="text" name="Year" /></td>
</tr>
<tr>
<td>Course:</td>
<td><input type="text" name="Course" /></td>
</tr>
<tr>
<td> </td>
<td align="right"><input type="submit" name="Go" value="Go" /></td>
</tr>
</table>
</form>
<br /> <br />
<?php
include_once("Student.class");
$s = new Student();
if(isset($_POST["FirstName"]))
{
$s->SetFirstName($_POST["FirstName"]);
$s->SetMiddleName($_POST["MiddleName"]);
$s->SetLastName($_POST["LastName"]);
$s->SetId($_POST["Id"]);
$s->SetYearLevel($_POST["Year"]);
$s->SetCourse($_POST["Course"]);
}
echo $s->DisplayStudent();
?>
</body>
</html>
In the following example, I have 3 files: Person.class, Student.class, and TestStudent.php. The Person.class has the following attributes: Last name, First name, and Middle name. It has one display method that returns an html code that displays the attributes of the person. The Student.class inherits the Person.class and also has its own attributes: Id, Year/Level, and Course. The TestStudent.php tests the Student class by asking for inputs of all six attributes using only the Student class and displays all of them in tabular form.
Person.class
<?php
class Person{
var $LastName="";}
var $FirstName="";
var $MiddleName="";
//Set/Get methods *********************************
function SetLastName($LastName){
$this->LastName = $LastName;
}
function GetLastName(){
return $this->LastName;
}
function SetFirstName($FirstName){
$this->FirstName = $FirstName;
}
function GetFirstName(){
return $this->FirstName;
}
function SetMiddleName($MiddleName){
$this->MiddleName = $MiddleName;
}
function GetMiddleName(){
return $this->MiddleName;
}
function DisplayPerson(){
$content=
"<table border=1 cellpadding=1>".
"<tr>"."<td>first name</td>"."<td>middle name</td>"."<td>last name</td></tr><tr>".
"<td>".$this->FirstName."</td>"."<td>".$this->MiddleName."</td>"."<td>".$this->LastName."</td>".
"</tr>"."</table>";
return $content;
}
?>
Student.class
<?php
include_once("Person.class");
Class Student extends Person{
var $Id;
var $YearLevel;
var $Course;
//Set/Get methods *********************************
function SetId($Id){
$this->Id = $Id;
}
function GetId(){
return $this->Id;
}
function SetYearLevel($YearLevel){
$this->YearLevel = $YearLevel;
}
function GetYearLevel(){
return $this->YearLevel;
}
function SetCourse($Course){
$this->Course = $Course;
}
function GetCourse(){
return $this->Course;
}
function DisplayStudent(){
$content = $this->DisplayPerson().
"<table border=1 cellpadding=1>".
"<tr>"."<td>Id</td>"."<td>Year Level</td>"."<td>Course</td></tr><tr>".
"<td>".$this->Id."</td>"."<td>".$this->YearLevel."</td>"."<td>".$this->Course."</td>".
"</tr>"."</table>";
return $content;
}
}
?>
TestStudent.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Sans Titre</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="generator" content="HAPedit 3.1">
</head>
<body bgcolor="#FFFFFF">
<form name="myform" method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>" >
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="FirstName" /> </td>
</tr>
<tr>
<td>Middle Name:</td>
<td><input type="text" name="MiddleName" /></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="LastName" /> </td>
</tr>
<tr>
<td>Id:</td>
<td><input type="text" name="Id" /></td>
</tr>
<tr>
<td>Year:</td>
<td><input type="text" name="Year" /></td>
</tr>
<tr>
<td>Course:</td>
<td><input type="text" name="Course" /></td>
</tr>
<tr>
<td> </td>
<td align="right"><input type="submit" name="Go" value="Go" /></td>
</tr>
</table>
</form>
<br /> <br />
<?php
include_once("Student.class");
$s = new Student();
if(isset($_POST["FirstName"]))
{
$s->SetFirstName($_POST["FirstName"]);
$s->SetMiddleName($_POST["MiddleName"]);
$s->SetLastName($_POST["LastName"]);
$s->SetId($_POST["Id"]);
$s->SetYearLevel($_POST["Year"]);
$s->SetCourse($_POST["Course"]);
}
echo $s->DisplayStudent();
?>
</body>
</html>
Comments