Abstract Classes in PHP
In PHP, abstract classes are essential for creating a flexible and structured object-oriented architecture. Understanding how abstract classes work can help you write more efficient and maintainable code. In this guide, we’ll explore abstract classes step by step, with relevant examples and SEO-friendly keywords to boost your understanding.
Step 1: What is an Abstract Class?
An abstract class in PHP is declared using the abstract keyword. It cannot be instantiated, meaning you cannot create an object directly from it. Instead, abstract classes serve as blueprints for other classes that extend them.
Syntax:
abstract class MyClass {
// class body
}
Attempting to create an object from an abstract class will result in an error:
$obj = new MyClass; // Error: Cannot instantiate abstract class
This error occurs because abstract classes are designed to be extended by other classes, not used directly.
Step 2: Properties and Methods in Abstract Classes
An abstract class can contain properties, constants, and both normal and abstract methods. However, if a class contains even one abstract method, it must be declared abstract.
Example:
abstract class MyClass {
abstract function myAbstractMethod($arg1, $arg2);
function myMethod() {
echo "Hello";
}
}
In the above example, myAbstractMethod() is abstract, meaning it has no body and must be implemented by any class that extends MyClass.
Step 3: Extending Abstract Classes
When another class extends an abstract class, it must implement all the abstract methods. Failure to do so will lead to an error.
Example:
<?php
abstract class MyClass {
abstract function myAbstractMethod($arg1, $arg2);
function myMethod() {
echo "Hello";
}
}
class NewClass extends MyClass {
function newMethod() {
echo "World";
}
}
$m1 = new NewClass;
$m1->myMethod();
?>
This code will result in an error because NewClass does not implement the abstract method myAbstractMethod().
Step 4: Implementing Abstract Methods
To avoid errors, the child class must provide a concrete implementation for every abstract method declared in the parent abstract class.
Correct Example:
<?php
abstract class MyClass {
abstract function myAbstractMethod($arg1, $arg2);
function myMethod() {
echo "Hello";
}
}
class NewClass extends MyClass {
function myAbstractMethod($arg1, $arg2) {
echo "Arguments: $arg1, $arg2";
}
}
$m1 = new NewClass;
$m1->myAbstractMethod(1, 2);
?>
In this example, NewClass implements myAbstractMethod(), making the code valid.
Step 5: Example of Abstract Class in Practice
Let’s consider a real-world example where an abstract class is used to calculate the percentage of marks for a student.
Example:
<?php
abstract class Marks {
protected int $m1, $m2, $m3;
abstract public function percent(): float;
}
class Student extends Marks {
public function __construct($x, $y, $z) {
$this->m1 = $x;
$this->m2 = $y;
$this->m3 = $z;
}
public function percent(): float {
return ($this->m1 + $this->m2 + $this->m3) * 100 / 300;
}
}
$s1 = new Student(50, 60, 70);
echo "Percentage of marks: ". $s1->percent();
?>
//Output
Percentage of marks: 60
This code demonstrates how an abstract class can be used as a blueprint, while the Student class implements the percent() method to calculate the percentage.
Step 6: Abstract Classes vs Interfaces in PHP
Although abstract classes and interfaces are similar, they have some key differences:
Abstract Class | Interface |
Use abstract keyword | Use interface keyword |
Can have both normal and abstract methods | Must declare methods with no body |
Can include public, private, or protected properties | Cannot declare properties |
Must be extended by another class | Must be implemented by another class |
Can have constants | Only method declarations allowed |
The Power of Abstract Classes in PHP
Abstract classes are crucial for creating well-structured and maintainable code in PHP. They provide a way to enforce certain methods to be implemented in child classes, ensuring a consistent structure across your codebase. By understanding the use of abstract classes, you can create flexible and reusable code with ease.
Keep Learning 🙂