Skip to main content

Simple Inheritance in PHP

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{
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

Popular posts from this blog

How to register a business name

Attending business summits and conferences is a big help to those who belong to the quite "techy" (technological or technical) industry. Being a graduate of one, I had less knowledge in the field of entrepreneurship. Enrolling myself in business administration gave me quite the knowledge to be a part of the business world and thus improve my entrepreneurial skills. I now would like to share this information that I got familiar with (and I managed to get a copy of the entire process from the 6th Mindanao ICT Congress): How to Register a Business Name (in the Philippines) ----------------------------------------------------------------------------------------------- SINGLE PROPRIETORSHIP Applicant must secure 2 copies of registration form and pay Php 300.00 (rate may change) for single proprietorship registration processing fee. The registration shall be valid for five (5) years. A surcharge of Php 100.00 is imposed if renewal is filed beyond the three (3) month grace period, c...

Adding a Footer to the DataGridView component

I have been searching for sites and forums that would give me a any hint on having a footer on the .net DataGridView control. It was frustrating. I found some, but not what I was looking for. I use windows forms. It would have been easier if I was into web. I decided to create one for myself. It's not complete, but it works with me. It needs improvement and I hope that some programmers who might pass through this blog will help me with it :D. Limitations: Cannot set Footer values during design time. Can sometimes hide a row when scrolled to the last item in the grid. What I did was just create a user control that inherits the DataGridView control and add a StatusStrip to act as the footer. public partial class MyDataGridView : DataGridView { public StatusStrip Footer { get { return (StatusStrip)this.Controls["Footer"]; } } private bool _footerVisible; [Browsable(false)] /// /// Sets or Gets the va...

Using Crystal Reports 10 with C#.net and Firebird

C# express doesn't include a report designer or viewer. Reports however, is very much needed when creating a business software. Since C# express doesn't include a report designer, we need to find other means. One is to use a free report such as MyNeoReport. This however may not work under many circumstances. The other alternative would be to use a proven report engine and designer-Crystal Report. Crystal Report has been used by many developers (in our city). However, using a free programming language and IDE, and a free database is very limiting. Not much information can be gathered on the net either (with regards to reporting as of this writing). Here's a way to use Crystal Reports using Firebird database and C# Express as software development IDE: Pre-requisites: C# Express 2005 EMS SQL Manager 2005 for InterBase & Firebird Lite Crystal Reports 10 Create the following database: Name: TestDB1 Tables: TESTTABLE1 Columns:  ID - PK, INTEGER,AUTOINCREMENT DES...