← Back to PHP Course | Chapter 8: OOP | Lesson 7 of 14

PHP Abstract Classes

An abstract class is like a half-finished blueprint that you cannot build directly. It sets out some parts and leaves others for the child classes to complete.
Syntax
php
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
<?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();
?>

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
<?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();
?>

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
<?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();
?>

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
<?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();
?>

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
<?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();
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to create an object of an abstract class with new, which is a fatal error.
  2. Not implementing every abstract method in the child class, which makes the child abstract too.
  3. Giving an abstract method a body, which is not allowed.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.