Inheritance and Abstract Class Concept with PHP 5
Mayıs 23, 08 by aligorkemSetting Up Development Environments
1. Zend Development Environment (Zend Studio)
2. Apache Web Server (Includes Zend Studio)
3. PHP 5 (Includes Zend Studio)
Contents
1. Inheritance
2. Screenshots
3. Abstract Class Consept
3. Screenshots
4. Download sample codes
Inheritance
A Student has all the same features as a Person. The only difference is that the Student class have TakeExam method. If we want to create a new class, we can create another class that extends Person, We’ll call this class “Worker”.
Person.php
Class Person
{
//Default value is empty
private $_name = "";
private $_surName = "";
function __construct( $name, $surName)
{
$this->_name= $name;
$this->_surName = $surName;
}
function getName() {
return $this->_name;
}
function getSurName() {
return $this->_surName;
}
}
?>
Student.php
require_once "Person.php";
Class Student Extends Person
{
//Default value is empty
private $_result = "";
function __construct( $name, $surName, $result)
{
//parameters pass to base class
parent::__construct($name,$surName);
//We set of the values
$this->_result = $result;
}
function __destruct()
{
}
function TakeExam() {
return $this->_result;
}
}
?>
Index.php
require_once "Student.php";
$studentJake = new Student("Jake","Plummer","Failed");
$name = $studentJake->getName();
$surName = $studentJake->getSurName();
$examResult = $studentJake->TakeExam();
$studentJohn = new Student("John","Lennon","Succeeded");
?>
Name : <?= $name ?> <br>
SurName : <?= $surName ?> <br>
Exam Result : <?= $examResult ?> <br><br>
Name : <?= $studentJohn->getName() ?> <br>
SurName : <?= $studentJohn->getSurName() ?> <br>
Exam Result : <?= $studentJohn->TakeExam() ?> <br>

Abstract Class Concept
A class abstract Person might be specified as abstract to represent the general abstraction of a Person, as creating instances of the class would not be meaningful. An abstract method is a method that is declared without an implementation.
Person.php
//define abstract method
abstract protected function GetCustomIDNumber();
Student.php
//implement abstract method and generate random number
function GetCustomIDNumber()
{
srand((double)microtime()*1000000);
return rand(0,100);
}











