Overview
Abstract classes in PHP serve as blueprints for other classes. They cannot be instantiated directly and often contain abstract methods that must be implemented by child classes, ensuring a set of functionalities are uniformly available while allowing for specific implementations.
Understanding Abstract Classes
In PHP, abstract classes are defined using the abstract
keyword. They can have properties and methods like any other class. However, one of the key features of an abstract class is that it may contain abstract methods — these are methods without an actual implementation. Think of an abstract class as a partial template; it requires subclasses to fill in the gaps.
<?php
abstract class Vehicle {
protected $fuelType;
public function setFuelType($fuelType) {
$this->fuelType = $fuelType;
}
abstract public function startEngine();
}
?>
In this example, any class that extends Vehicle
will have to implement the startEngine()
method, as it’s abstract.
Implementing Abstract Classes
Let’s extend our Vehicle
class with a concrete subclass:
<?php
class Car extends Vehicle {
public function startEngine() {
echo 'Engine started with ' . $this->fuelType;
}
}
$myCar = new Car();
$myCar->setFuelType('petrol');
$myCar->startEngine(); // Output: Engine started with petrol
?>
This subclass provides the specific implementation for the originally abstract startEngine()
method.
Advanced Inheritance and Abstract Classes
Abstract classes can also have defined methods, which can contain functionality that will be shared by all subclasses. Abstract methods ensure consistency in method signatures.
<?php
abstract class Animal {
protected $species;
public function __construct($species) {
$this->species = $species;
}
public function getSpecies() {
return $this->species;
}
abstract public function communicate();
}
class Dog extends Animal {
public function communicate() {
echo 'Bark bark';
}
}
class Cat extends Animal {
public function communicate() {
echo 'Meow meow';
}
}
$dog = new Dog('Canine');
$cat = new Cat('Feline');
$dog->communicate(); // Output: Bark bark
$cat->communicate(); // Output: Meow meow
?>
Note that the Animal
class includes a constructor and a defined method, getSpecies()
, alongside the abstract method communicate()
. Each subclass must implement the abstract method, though they share the constructor and defined methods from Animal
.
Using Interfaces with Abstract Classes
It’s possible to combine abstract classes with interfaces to provide an even greater level of abstraction and contract-based programming.
<?php
interface Runnable {
public function run();
}
abstract class Machine implements Runnable {
// Even though this class is abstract, by implementing an interface,
// it must provide implementation for all interface methods or be declared abstract.
abstract public function run();
}
class Printer extends Machine {
public function run() {
echo 'Printing...';
}
}
$printer = new Printer();
$printer->run(); // Output: Printing...
?>
The Machine
class implements the Runnable
interface but remains abstract, nudging any subclasses to provide their implementation of the run()
method.
Conclusion
This tutorial has explored the significance and usefulness of abstract classes in PHP. By implementing abstract methods and utilizing inheritance, developers can create robust and flexible class hierarchies. Mastering abstract classes will ultimately lead to cleaner, more maintainable, and more scalable PHP code.