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”
Popular Articles:
- PHP5 Tutorial – __wakeup() Magic Method
- Writing HTML Scrapper in PHP
- Increase PHP Script Memory Limit
- OOPS in PHP 5 Tutorial – Static Keyword
- Reading Remote URL HTML Source in PHP
- OOPS in PHP5 – Declaring Class and Initializing Class Object
- OOPS in PHP5 – __construct() Method
- PHP5 Tutorial – __unset() Magic Method
- OOPS in PHP 5 Tutorial – Function Overriding
- Generate Unique SessionId in PHP



































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).
Hi,Really Helpful esp. your Articles about PHP5,i hope it will very useful for PHP Programmer who come from PHP 4 vesion into Version 5
Very well explained… Same can be enhanced by writing on “Why we need an abstract class? with vary practical example example”
Hey its rally very very good example which makes abstract class concept much easier to understand.
Thanks
In example 2 even if you dont write following lines
$a = new EmployeeData(“Hitesh”,”24″);
$a->outputData();
It will show you an error.
Hi Hitesh, great explanation.. keep it up.. !!
Can you pls tell me ..
I think ABSTRACT class is used for same function in different class used for different purposes like polymorphism.. is it true?
Why we need an abstract class?
Hi Manoilayans,
We require abstract to define an contract that an class with implement. Also abstract can hold constants and non-abstract methods that an class can use to perform operation.
Example:
If we define an abstract class called 4-Wheeler that will have Engine, Tyres etc. Now we can take any 4-wheeler car that will have to implement the abstract methods defined in the abstract class. The abstract class is not concerned about the implementation.
Hope this will solve your query.
Thanks,
Hitesh Agarwal
what is basic different between abstract class & normal class with inheritance.
?
@debranjan bera
This is what I’ve been particularly trying to grasp. The basic difference is that an abstract class cannot be instantiated while a normal class used as a base can. Indeed, there are plenty of situations where I’ve created a base class that was never meant to be used directly, only inherited from, so an abstract class makes sense in that regard.
However, I still don’t understand why it’s considered good practice to NOT implement anything in the abstract class. To me, that defeats the whole purpose of a base class – to be able to define things once and once only for use throughout a program. Why declare something in the abstract class only to re-declare it in every subclass? Sure, it allows you to look at the abstract class for a quick visual overview of what all subclasses will contain, but it makes little sense to implement nothing there simply to follow convention.
The code examples in this page make use of implementation in the abstract class, so I’m not seeing the value behind this “best practice” I’ve been reading about. Any thoughts?
First of all public/protected variables you declare in abstract class is automatically inherited in base class and you don’t need to declare the variable at all location.
Abstract also allows you to perform operation specific to an object, say for e.g. Car is an abstract class, car will have steering, wheels, window etc. So abstract class will implements all the common operation that car performs like how steering wheel works etc, but the implementation of how gears works(automatic/manual) can be inside base class that extends the abstract car class.
Hope this answers your query.
hi Hitesh,
excellent explanation about abstract class,
and one more thing, i hope u will be helpful,
sir, i want complete explanation about abstract method,
And Why we need an abstract method ?
explain me with examples one or two please ,,,,,,,,,,,,
Thank u,
uday victory
hi,
can you tell me where i implement abstract class in our codding please tell me an example
Hi Nirvikar,
There are various scenarios where you would require abstract class. Common example would Car Object, Car has wheels, steering wheel, door all these things are common to all the Cars so this can be abstract and can be inside abstract class but functionality like braking system, engine starting mechanism can vary from car to car, this can be written in abstract class as abstract methods and it’s implementation would be inside concrete class.
Hope this helps in understanding the concept of abstract class.
Thanks,
Hitesh Agarwal