PHP Abstract Classes

PHP abstract classes provide a way to define a base class that cannot be instantiated on its own but can be extended by other classes.

This article will provide a comprehensive overview of PHP abstract classes. Here we are going to learn how to define abstract classes and methods, as well as how to use them in our coding projects.

Abstract classes are an essential part of object-oriented programming, allowing you to write more flexible and maintainable code.

PHP abstract class is characterized by the fact that it cannot be instantiated directly in an object-oriented programming (OOP) environment, but serves as a template from which other classes can inherit their functionality.

The abstract class consists of abstract methods that do not have any implementation of their own, and they must be implemented by any concrete subclass that inherits from the abstract class.



PHP Abstract Classes and Methods

The abstract class and methods are the ones in which a parent class has a named method, but that method needs to be filled out by the child class(es).

The abstract class is defined as the class which contains at least one abstract method. Methods declared in an abstract manner will not be coded; instead, they will be declared but not implemented.

Using the abstract keyword, a class or method can be defined as being abstract:

Syntax:

<?php
abstract class MainClass {
abstract public function method1();
abstract public function method2($name, $color);
}
?>

When inheriting from an abstract class with a method name that is the same as the parent class, the child class method must have a less restricted access modifier or the same access modifier as the parent class.

In other words, if the abstract method of the parent class is defined as protected, then the method of the child class must be either defined as protected or public. It is also necessary for the type of argument and the number of arguments required to be the same.

In addition to their parent classes, child classes can take optional arguments.

Hence, when a child class inherits from an abstract class, the following rules have to be followed:

  • Methods in the child class must be implemented with the same name as their parent class method, which must be redeclared.
  • This method must have the same or a less restrictive access modifier as the method of the parent class.
  • There must be the same number of arguments required for each method. In addition to the mandatory arguments, the child class may also accept optional arguments

In order to understand this concept better, let us look at an example:

Example: 

<?php // Parent class abstract class Shape { public $name; public function __construct($name) { $this->name = $name; } abstract public function message(); } // Child classes class Rectangle extends Shape { public function message() { return "This is a $this->name shape!"; } } class Triangle extends Shape { public function message() { return "This is a $this->name shape!"; } } class Circle extends Shape { public function message() { return "This is a $this->name shape!"; } } // Create objects from the child classes $rect = new Rectangle("Rectangle"); echo $rect->message(); echo "<br>"; $triangle = new Triangle("Triangle"); echo $triangle->message(); echo "<br>"; $circle = new Circle("Circle"); echo $circle->message(); ?>

Example Explanation

The Shape class is the parent class of the Rectangle, Triangle, and Circle classes, which are all inherited from it.

By inheriting both the public $name property and the public __construct() method from the Shape class, it is possible for Rectangle, Triangle, and Circle classes to use the public $name property and the public __construct() method of the Shape class.

Despite this, message() is an abstract method and it should be defined in all the child classes as well, and they should return a string when called.

In the following example, we will demonstrate a situation where there is an argument provided for an abstract method:

Example: 

<?php//Parent Classes abstract class Person { public $name; public function __construct($name) { $this->name = $name; } abstract public function greeting(); }// Child classes class Programmer extends Person { public function greeting(){ return "Hello world from programmer and my name is " . $this -> name; } } class Student extends Person { public function greeting(){ return "Hello! from a student and my name is $this->name!"; } }class Employee extends Person { public function greeting() { return "Hello! from an employee and my name is $this->name! "; } }// create instances of the child classes $programmer = new Programmer("Denis"); echo $programmer->greeting(); echo "<br>"; $student = new Student("John"); echo $student->greeting(); echo "<br>"; $employee = new Employee("Alex"); echo $employee->greeting();?>
Another example is where we have an abstract method in a parent class that has an argument, and then we have an abstract method in a child class that has one optional arguments that aren’t defined in the abstract method of the parent class:

Example: 

<?php//Parent Classes abstract class Person { public $name;abstract public function greeting($name); }// Child classes class Programmer extends Person { public function greeting($name, $language = "PHP"){ return "Hello world from programmer and my name is $name and working as $language developer"; } } class Student extends Person { public function greeting($name, $class = "8th"){ return "Hello! from a student and my name is $name! and i am studying in $class class"; } }class Employee extends Person { public function greeting($name, $yearOfExp = 5) { return "Hello! from a employee and my name is $name! and i have $yearOfExp year of experience"; } }// create instances of the child classes $programmer = new Programmer(); echo $programmer->greeting("Denis"); echo "<br>"; $student = new Student(); echo $student->greeting("John"); echo "<br>"; $employee = new Employee(); echo $employee->greeting("Alex");?>

Why Use PHP Abstract Classes?

Abstract classes are useful in PHP for several reasons:

  • PHP abstract classes provide a way to define common functionality that can be reused across multiple subclasses.
  • Abstract classes allow us to create a hierarchy of related classes, making it easier to write code that works with multiple related objects.
  • Abstract classes provide a way to encapsulate implementation details within a base class, reducing code duplication and improving maintainability.
  • PHP abstract classes can be extended by other classes to add additional functionality, making code more flexible and adaptable to changing requirements.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *