OOPS in PHP 5 Tutorial - Abstract Class
This article will guide you on the Abstract Class used in PHP5. Also it will give an insight on what abstract class is all about when we talk about Object Oriented Programming Concept.
What is an Abstract Class?
An abstract class defines the basic skeleton for the class. It contains attributes and members but some members are incomplete and is waiting for some other class to extend it through inheritance so that the derived class provides a full functionality for the incomplete methods. A abstract class cannot be instantiated and it can only be extended. A class prefix with “abstract” keywords are abstract class. If a method is defined as abstract then it cannot be declared as private (it can only be public or protected).
Syntax:
< ? abstract class classname { //attributes and methods . . abstract function methodname } class derived extends classname { function methodname } ?>
Example 1 - Basic Abstract Class Usage
< ? abstract class employee { protected $empname; protected $empage; function setdata($empname,$empage) { $this->empname = $empname; $this->empage = $empage; } abstract function outputData(); } class EmployeeData extends employee //extending abstract class { function __construct($name,$age) { $this->setdata($name,$age); } function outputData() { echo $this->empname; echo $this->empage; } } $a = new EmployeeData("Hitesh","24"); $a->outputData(); ?>
Output:
Hitesh
24
Explanation for the Example 1:
- Here i have made employee class an abstract class
- Employee class has 2 data members $empname and $empage. It also has a abstract method outputData() and public method setData()
- Now i am extending the employee class in EmployeeData. It contains the functionality for the abstract method outputData defined in employee class
Example 2 - Abstract Class Error
< ? abstract class employee { protected $empname; protected $empage; function setdata($empname,$empage) { $this->empname = $empname; $this->empage = $empage; } abstract function outputData(); } class EmployeeData extends employee { function __construct($name,$age) { $this->setdata($name,$age); } } $a = new EmployeeData("Hitesh","24"); $a->outputData(); //Will generate error as the EmployeeData class doesn't has the abstract method defined ?>
Output
This will give you an error “Class EmployeeData contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (employee::outputData)”
Example3 - Multiple Abstract Class
< ? abstract class employee { protected $empname; protected $empage; function setdata($empname,$empage) { $this->empname = $empname; $this->empage = $empage; } abstract function outputData(); } abstract class EmployeeData extends employee { abstract function setDataForEmployee(); } class Payment extends EmployeeData { function setDataForEmployee() { //Functionality } function outputData() { echo "Inside Payment Class"; } } $a = new Payment(); $a->outputData(); ?>
Output: “Inside Payment Class”
Similar Posts
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.


An abstract class defines the basic skeleton for the class. It contains attributes and members but some members are incomplete and is waiting for some other class to extend it so that the derived class provides a full functionality for the incomplete methods. A abstract class cannot be instantiated and it can only be extended. A class prefix with “abstract” keywords are abstract class. If a method is defined as abstract then it cannot be declared as private (it can only be public or protected).