0
0
PHPprogramming~5 mins

Abstract classes and methods in PHP

Choose your learning style9 modes available
Introduction

Abstract classes let you create a basic plan for other classes to follow. Abstract methods are like empty tasks that child classes must fill in.

When you want to make sure certain methods exist in all child classes.
When you have a general idea but want child classes to provide specific details.
When you want to prevent creating objects from a base class directly.
When you want to share some code but force other parts to be customized.
When designing a system with different types of similar objects.
Syntax
PHP
<?php
abstract class Animal {
    abstract public function makeSound();

    public function sleep() {
        echo "Sleeping...\n";
    }
}

class Dog extends Animal {
    public function makeSound() {
        echo "Bark!\n";
    }
}
?>

An abstract class is declared with the keyword abstract.

Abstract methods have no body and must be implemented by child classes.

Examples
Different child classes implement the abstract method in their own way.
PHP
<?php
abstract class Vehicle {
    abstract public function startEngine();
}

class Car extends Vehicle {
    public function startEngine() {
        echo "Car engine started\n";
    }
}

class Bike extends Vehicle {
    public function startEngine() {
        echo "Bike engine started\n";
    }
}
?>
You cannot create an object from an abstract class directly.
PHP
<?php
abstract class Shape {
    abstract public function area();
}

// Edge case: No child class created, so no object can be made from Shape
// Trying to create Shape directly will cause an error.
?>
Child classes must implement all abstract methods or PHP will throw an error.
PHP
<?php
abstract class Printer {
    abstract public function printDocument();
}

class SimplePrinter extends Printer {
    public function printDocument() {
        echo "Printing document...\n";
    }
}

// Edge case: If SimplePrinter does not implement printDocument, PHP will error.
?>
Sample Program

This program shows an abstract class Employee with an abstract method calculateSalary. Two child classes implement this method differently. We create objects and show their salaries.

PHP
<?php
abstract class Employee {
    protected string $name;

    public function __construct(string $name) {
        $this->name = $name;
    }

    abstract public function calculateSalary(): float;

    public function display() {
        echo "Employee: {$this->name}\n";
    }
}

class FullTimeEmployee extends Employee {
    private float $monthlySalary;

    public function __construct(string $name, float $monthlySalary) {
        parent::__construct($name);
        $this->monthlySalary = $monthlySalary;
    }

    public function calculateSalary(): float {
        return $this->monthlySalary;
    }
}

class PartTimeEmployee extends Employee {
    private float $hourlyRate;
    private int $hoursWorked;

    public function __construct(string $name, float $hourlyRate, int $hoursWorked) {
        parent::__construct($name);
        $this->hourlyRate = $hourlyRate;
        $this->hoursWorked = $hoursWorked;
    }

    public function calculateSalary(): float {
        return $this->hourlyRate * $this->hoursWorked;
    }
}

// Create employees
$fullTimeEmployee = new FullTimeEmployee("Alice", 3000.00);
$partTimeEmployee = new PartTimeEmployee("Bob", 20.00, 80);

// Display and calculate salaries
$fullTimeEmployee->display();
echo "Salary: $" . $fullTimeEmployee->calculateSalary() . "\n";

$partTimeEmployee->display();
echo "Salary: $" . $partTimeEmployee->calculateSalary() . "\n";
?>
OutputSuccess
Important Notes

Time complexity: Implementing abstract methods does not affect runtime complexity; it is about design.

Space complexity: No extra space is used by abstract classes themselves.

Common mistake: Forgetting to implement all abstract methods in child classes causes errors.

Use abstract classes when you want to force child classes to have certain methods but also share some code.

Summary

Abstract classes provide a base plan that cannot be used directly.

Abstract methods are empty methods that child classes must fill in.

Child classes must implement all abstract methods or PHP will throw an error.