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.
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:
Vehicle
is an abstract class. It defines an abstract methodmove()
and a regular methodstop()
.Bicycle
extendsVehicle
and provides the implementation formove()
.- You can call both
move()
andstop()
on aBicycle
object.
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
abstract
keyword. - 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
abstract
keyword. - 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.";
}
}