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.
Abstract classes and methods in 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.
<?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"; } } ?>
<?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. ?>
<?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. ?>
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 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"; ?>
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.
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.