PHP OOP Abstract Classes Explained

Last Updated: 24 Jul, 2024

What is an Abstract Class in PHP?

A class that has minimum one abstract method is called as abstract class. An abstract class is created with abstract keyword and it cannot be instantiated by itself. Abstract methods can only declare the method's signature; they cannot define the actual implementation. An abstract class can also have both abstract and non abstract methods.

Points to note:

  • An abstract class must be defined with the abstract keyword.
  • An abstract class must have minimum one abstract method.
  • An abstract class may have both abstract and non abstract methods and follows partial abstraction.
  • We cannot instantiate an abstract class.
  • Typically, an abstract class defines an interface for other classes that will extend abstract class.
  • An abstract class may also have constructor.
  • An abstract class can also have methods and properties similar to a regular class.
  • Like abstract class, an abstract method is declared by using abstract keyword and only offers method signature.
  • Abstract class does not support for multiple inheritance.

When a child class extends an abstract class, it needs to follow the following rules:

  • It must re-declare all inherited abstract methods with the same name.
  • It must implement all inherited abstract methods of abstract class or it must be declared as abstract class.
  • It must define all the abstract methods with either same or less restricted access modifier.
  • No of arguments for abstract methods should be same. Child class can also have additional arguments if necessary.

Example 1: An abstract class must be declared with abstract keyword and cannot be instantiated on itself

<?php
abstract class Vehicle
{
    abstract function getBrand(): string;
    abstract function getModel(): string;
}
class Car extends Vehicle
{
    public $brand;
    public $model;
    public function __construct($brand, $model)
    {
        $this->brand = $brand;
        $this->model = $model;
    }
    public function getBrand(): string
    {
        return $this->brand;
    }
    public function getModel(): string
    {
        return $this->model;
    }
}
$obj = new Car('TATA', 'Safari');
$properties = array(
    'Brand' => $obj->getBrand(),
    'Model' => $obj->getModel(),
);
print_r($properties);
/* OUTPUT
Array
(
    [Brand] => TATA
    [Model] => Safari
)
*/
?>

Example 2: A class that extends an abstract class must implements all abstract methods inherited from abstract class or itself declread as abstract.

<?php
abstract class Vehicle
{
    abstract function getBrand(): string;
    abstract function getModel(): string;
}
abstract class Car extends Vehicle
{
    abstract function getName(): string;
}
class MyCar extends Car
{
    public $brand;
    public $model;
    public $name;
    public function __construct($brand, $model, $name)
    {
        $this->brand = $brand;
        $this->model = $model;
        $this->name = $name;
    }
    public function getBrand(): string
    {
        return $this->brand;
    }
    public function getModel(): string
    {
        return $this->model;
    }
    public function getName(): string
    {
        return $this->name;
    }
}
$obj = new MyCar('Toyota', 'Innova', 'Innova Crysta');
$properties = array(
    'Brand' => $obj->getBrand(),
    'Model' => $obj->getModel(),
    'Name' => $obj->getName(),
);
print_r($properties);
/* OUTPUT
Array
(
    [Brand] => Toyota
    [Model] => Innova
    [Name] => Innova Crysta
)
*/
?>

Example 3: An abstract class can also have constructor in it as shown in below example.

<?php
abstract class Vehicle
{
    public $brand;
    public $model;
    public function __construct($brand, $model)
    {
        $this->brand = $brand;
        $this->model = $model;
    }
    abstract function getBrandAndModel(): string;
}
class Car extends Vehicle
{
    public function getBrandAndModel(): string
    {
        return "Hi, I am '$this->model' from '$this->brand'.";
    }
}
$details = array();
$obj1 = new Car('Toyota', 'Innova');
array_push($details, $obj1->getBrandAndModel());
$obj2 = new Car('Tata', 'Safari');
array_push($details, $obj2->getBrandAndModel());
$obj3 = new Car('Hyundai', 'Venue');
array_push($details, $obj3->getBrandAndModel());
print_r($details);
/* OUTPUT
Array
(
    [0] => Hi, I am 'Innova' from 'Toyota'.
    [1] => Hi, I am 'Safari' from 'Tata'.
    [2] => Hi, I am 'Venue' from 'Hyundai'.
)
*/
?>

 

Thank You, Please Share.

Recommended Posts

IMAGE

PHP OOP Interfaces Explained

An Interface allows you to create programs that specifies which methods a class must implement, without defining how those methods are implemented.

IMAGE

PHP Magic Methods Explained

Magic Methods are special types of methods in PHP that allows you to perform certain special actions. These methods start with a double underscore (__).

IMAGE

PHP OOP Traits Explained

Traits in PHP, are a medium of implementing code reuse and allow us to reuse sets of functions freely in several independent classes.