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:


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

Custom Search

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.

Comments

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”

Leave a comment

(required)

(required)