PHP Abstract Classes
In this page:
abstract class ClassName {
abstract function method_name(); // no body
}
class ChildClass extends ClassName {
function method_name() {
// implementation required
}
}
What is an Abstract Class?
An abstract class, declared with the abstract keyword, cannot be instantiated directly with new — it exists only to be extended, serving as a partial template that defines shared structure for its subclasses.
Example: What is an Abstract Class?
<?php
// Define the class `Shape`
abstract class Shape {
// Define the function `area` with no parameters
abstract function area();
}
// Define the class `Square`, inheriting from `Shape`
class Square extends Shape {
// Define the function `area` with no parameters
function area() {
// Return `16` from this function
return 16;
}
}
// Create a new `Square` instance, stored in `$sq`
$sq = new Square();
// Print `$sq->area()` to the output
echo $sq->area();
?>
Login to try C/C++/Java/PHP code in the editor
Defining Abstract Methods
abstract methods inside an abstract class have no body — just a signature — forcing every concrete (non-abstract) subclass to provide its own implementation, or PHP will raise a fatal error.
Example: Defining Abstract Methods
<?php
// Define the class `Shape`
abstract class Shape {
// Define the function `area` with no parameters
abstract function area();
}
// Define the class `Circle`, inheriting from `Shape`
class Circle extends Shape {
// Define the function `area` with no parameters
function area() {
// Return `3.14 * 5 * 5` from this function
return 3.14 * 5 * 5;
}
}
// Create a new `Circle` instance, stored in `$c`
$c = new Circle();
// Print `$c->area()` to the output
echo $c->area();
?>
Login to try C/C++/Java/PHP code in the editor
Implementing Abstract Methods
Abstract classes can still contain regular, fully-implemented methods and properties alongside their abstract ones, letting you share common logic while requiring subclasses to fill in the parts that genuinely differ.
Example: Implementing Abstract Methods
<?php
// Define the class `Shape`
abstract class Shape {
// Define the function `describe` with no parameters
function describe() {
// Print "This is a shape with area: " to the output
echo "This is a shape with area: ";
}
// Define the function `area` with no parameters
abstract function area();
}
// Define the class `Square`, inheriting from `Shape`
class Square extends Shape {
// Define the function `area` with no parameters
function area() {
// Return `9` from this function
return 9;
}
}
// Create a new `Square` instance, stored in `$sq`
$sq = new Square();
$sq->describe();
// Print `$sq->area()` to the output
echo $sq->area();
?>
Login to try C/C++/Java/PHP code in the editor
Abstract Classes with Regular Methods
Use an abstract class when several related classes should share both common state/behavior and a mandatory contract for a few specific methods, unlike a plain interface, which can't hold shared implementation.
Example: Abstract Classes with Regular Methods
<?php
// Define the class `Employee`
abstract class Employee {
// Define the function `clockIn` with no parameters
function clockIn() {
// Print "Clocked in\n" to the output
echo "Clocked in\n";
}
// Define the function `calculatePay` with no parameters
abstract function calculatePay();
}
// Define the class `Hourly`, inheriting from `Employee`
class Hourly extends Employee {
// Define the function `calculatePay` with no parameters
function calculatePay() {
// Return `15 * 40` from this function
return 15 * 40;
}
}
// Create a new `Hourly` instance, stored in `$emp`
$emp = new Hourly();
$emp->clockIn();
// Print `$emp->calculatePay()` to the output
echo $emp->calculatePay();
?>
Login to try C/C++/Java/PHP code in the editor
Parameter Matching Rules
A typical example is an abstract PaymentMethod class with a shared logTransaction() method plus an abstract processPayment() method, where CreditCard and PayPal subclasses each implement processPayment() their own way.
Example: Parameter Matching Rules
<?php
// Define the class `PaymentMethod`
abstract class PaymentMethod {
// Define the function `logTransaction` with no parameters
function logTransaction() {
// Print "Transaction logged\n" to the output
echo "Transaction logged\n";
}
// Define the function `processPayment` with no parameters
abstract function processPayment();
}
// Define the class `CreditCard`, inheriting from `PaymentMethod`
class CreditCard extends PaymentMethod {
// Define the function `processPayment` with no parameters
function processPayment() {
// Print "Processing credit card payment" to the output
echo "Processing credit card payment";
}
}
// Create a new `CreditCard` instance, stored in `$payment`
$payment = new CreditCard();
$payment->logTransaction();
$payment->processPayment();
?>
Login to try C/C++/Java/PHP code in the editor
- Trying to create an object of an abstract class with
new, which is a fatal error. - Not implementing every abstract method in the child class, which makes the child abstract too.
- Giving an abstract method a body, which is not allowed.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: