Abstract class in PHP appeared to provide a way to define a structure for related classes and don’t allow direct instantiation. They make sure that subclasses implement specific methods and improve code organization.
Table of Content
We will cover the following topics in this article:
- The basic definition of abstract classes in PHP.
- Extend abstract classes in PHP.
- The difference between abstract classes and interfaces.
Let’s start with the basic definition.
What is an Abstract Class in PHP?
An abstract class in PHP acts as a template for other classes. You can’t create an object directly from it.
It includes abstract methods that declare what a method should do but leave the actual implementation to the child classes. Abstract classes can also have regular methods with full code inside them.
Here is a quick example:
abstract class Vehicle {
// Abstract method (no implementation)
abstract public function move();
// Regular method
public function stop() {
echo "The vehicle stops.";
}
}
class Bicycle extends Vehicle {
public function move() {
echo "The bicycle pedals forward.";
}
}
$bicycle = new Bicycle();
$bicycle->move();
$bicycle->stop(); Output:
The bicycle pedals forward.The vehicle stops.
Here’s what happens:
Vehicleis an abstract class. It defines an abstract methodmove()and a regular methodstop().BicycleextendsVehicleand provides the implementation formove().- You can call both
move()andstop()on aBicycleobject.
Abstract classes make sure that child classes like Bicycle implement specific behaviors. It also allows shared functionality like stop().
Abstract classes in PHP define a base structure for related classes. They provide shared methods, while it requires subclasses to implement specific behaviors.
An abstract class uses the abstract keyword. It can contain both abstract methods (without a body) and concrete methods (with a body). Subclasses must implement all abstract methods.
Let’s move on to the following section to understand how to extend abstract classes in PHP.
How to Extend an Abstract Class in PHP
Create a subclass using the extends keyword. The subclass must implement all abstract methods from the parent class.
Here is an example:
abstract class Animal {
abstract public function makeSound();
public function sleep() {
return "Sleeping...";
}
}
class Dog extends Animal {
public function makeSound() {
return "Bark";
}
}
$dog = new Dog();
echo $dog->makeSound();
echo "\n";
echo $dog->sleep(); Output:
Bark
Sleeping...
Here are steps to extend abstract classes:
- Define an abstract class with the
abstractkeyword. - Include abstract methods (without a body) and concrete methods (with a body).
- Create a subclass with
extends. - Implement all abstract methods in the subclass.
- Instantiate the subclass and use its methods.
You must decide how to handle abstract class methods when you extend them in PHP. So, the difference between abstract and concrete methods helps you implement them correctly. Let’s move to the following section to see how key differences.
Abstract Methods vs. Concrete Methods in PHP
PHP supports both abstract and concrete methods in abstract classes. Each serves a different purpose in structuring code.
Abstract Methods:
- Declared with the
abstractkeyword. - Must be implemented in subclasses.
- Cannot have a body (no implementation).
- Enforce a required structure for all child classes.
Concrete Methods:
- Have a defined implementation.
- Can be inherited by subclasses.
- Allow code reuse.
- Subclasses can override them if needed.
Here are the key differences:
| Feature | Abstract Method | Concrete Method |
|---|---|---|
| Implementation | No body | Has a body |
| Requires Override | Yes | No |
| Code Reusability | No | Yes |
So, what are the differences between abstract classes and interfaces in PHP? Let’s answer this question in the following section.
Difference Between Abstract Classes and Interfaces in PHP
Abstract classes and interfaces set rules for other classes, but they work in different ways.
Abstract Classes:
- Can have abstract and concrete methods.
- Can have properties.
- Use
extends(single inheritance). - Methods can be
public,protected, orprivate. - Best for shared behavior and structure.
Interfaces:
- Only method signatures (before PHP 8.0).
- Cannot have properties.
- Use
implements(multiple inheritance). - Methods must be
public.
Wrapping Up
You learned how abstract classes define a base structure for related classes and enforce method implementation in PHP. You also saw how to extend abstract classes and compare abstract classes with interfaces.
Here is a quick recap:
- Abstract classes provide a structure for subclasses and can have both abstract and regular methods.
- It extends an abstract class and requires the child class to implement all abstract methods.
- Abstract vs. concrete methods—abstract methods must be implemented, while concrete methods provide default behavior.
- Abstract classes vs. interfaces—abstract classes allow properties and mixed methods, while interfaces enforce method signatures.
FAQ’s
What is an abstract class in PHP?
How do you define an abstract class in PHP?
abstract class Vehicle {
abstract public function move();
public function stop() {
echo "The vehicle stops.";
}
}
Can an abstract class have properties in PHP?
Can an abstract class have both abstract and concrete methods?
How do you extend an abstract class in PHP?
class Bicycle extends Vehicle {
public function move() {
echo "The bicycle pedals forward.";
}
}
What happens if a subclass does not implement an abstract method?
Can a class extend multiple abstract classes in PHP?
Similar Reads
PHP 8.4 released the array_any to test if at least one element matches a condition. Understand the array_any Function in…
The PHP object is an instance from the PHP class or the main data structure that is already been created…
PHP developers copied and pasted code across multiple files before inheritance came, which made updates difficult. They updated functions in…
Variables in PHP are general elements used for data storage and other manipulations. Global variables belong to a particular category…
PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose…
In this tutorial, we will explore a form of superglobal variable in PHP: $_ENV, which symbolizes how PHP reaches for…
PHP array_unshift helps you add new elements to appear before the existing ones. Understand the array_unshift function in PHP The…
You may need to get part of a text when you work with text in PHP. The mb_substr gives you…
The PHP do-while loop is a type of loop that executes a block of code at least once, and then…
PHP gives you many string tools. One of them is php str_repeat. This function repeats a texts as many times…