PHP Abstract Class

Sheeraz Gul Jun 30, 2022
PHP Abstract Class

Abstract classes are classes with at least one abstract method. This tutorial demonstrates how to create and use abstract classes in PHP.

PHP Abstract Class

In PHP, the abstract classes are declared using the abstract keyword, unlike the C++. The abstract class should contain at least one abstract method without any actual code; this method only has the name and parameters declared with the abstract keyword.

The purpose of an abstract class is to provide the kind of template to inherit from and force the inheriting class to implement the abstract methods. The abstract class is something between the pure interface and a regular class.

The interface can also be a special case of abstract classes where every method is abstract. The abstract class can never be instantiated, and if any class contains one abstract method, that class must be abstract.

The syntax for the abstract class is:

<?php
abstract class Demo
{
   abstract function Method_Name(Parameters);
}
?>

This syntax creates an abstract class with the abstract method Method_Name.

Let’s try an example for an abstract class in PHP:

<?php
// Parent abstract class
abstract class Employee {
    public $Employee_Name;
    public function __construct($Employee_Name) {
        $this->name = $Employee_Name;
    }
    abstract public function intro() : string;
}

// Child classes
class Jack extends Employee {
    public function intro() : string {
        return "Hello I am Project Manager at Delftstack! My Name is $this->name!";
    }
}

class Michelle extends Employee {
    public function intro() : string {
        return "Hi I am Human Resource Manager at Delftstack! My Name is $this->name!";
    }
}

class John extends Employee {
    public function intro() : string {
        return "Hey I am a Senior Developer at Delftstack! My Name is $this->name!";
    }
}

// Create objects from the child classes of abstract class
$Jack = new Jack("Jack");
echo $Jack->intro();
echo "<br>";

$Michelle = new Michelle("Michelle");
echo $Michelle->intro();
echo "<br>";

$John = new John("John");
echo $John->intro();
?>

Jack, Michelle, and John are inherited from the abstract class Employee, which means they can use the public Employee_Name property and the public __construct method from the Employee abstract class just because of the inheritance. But intro() is the abstract method that is why it is defined in all classes.

See output for the code:

Hello I am Project Manager at Delftstack! My Name is Jack!
Hi I am Human Resource Manager at Delftstack! My Name is Michelle!
Hey I am a Senior Developer at Delftstack! My Name is John!
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - PHP Class