PHP Abstract Classes
Creating blueprints for other classes
🎨 What are Abstract Classes?
Abstract classes are templates that cannot be instantiated directly. They define methods that child classes must implement, ensuring consistent structure across related classes.
<?php
// Simple abstract class example
abstract class Animal {
abstract public function makeSound();
}
?>
Key Concepts
Cannot Instantiate
Abstract classes cannot create objects directly
// This will cause error
$obj = new Animal();
Abstract Methods
Methods without implementation
abstract public function
makeSound();
Regular Methods
Can have normal methods too
public function sleep() {
echo "Sleeping...";
}
Child Classes
Must implement abstract methods
class Dog extends Animal {
// Must implement
}
🔹 Basic Abstract Class
Abstract classes serve as blueprints for child classes. They can contain both abstract methods (without body) and regular methods (with body). Child classes must implement all abstract methods to work properly.
<?php
// Define abstract class
abstract class Animal {
protected $name;
// Regular method
public function setName($name) {
$this->name = $name;
}
// Abstract method - no body
abstract public function makeSound();
}
// Child class must implement abstract methods
class Dog extends Animal {
public function makeSound() {
return "Woof! Woof!";
}
}
class Cat extends Animal {
public function makeSound() {
return "Meow! Meow!";
}
}
// Create objects
$dog = new Dog();
$dog->setName("Buddy");
echo $dog->makeSound(); // Woof! Woof!
$cat = new Cat();
$cat->setName("Whiskers");
echo $cat->makeSound(); // Meow! Meow!
?>
Output:
Woof! Woof!
Meow! Meow!
🔹 Abstract Class with Properties
Abstract classes can have properties and constructors just like regular classes. This allows you to share common data and initialization logic across all child classes while enforcing specific method implementations.
<?php
abstract class Shape {
protected $color;
public function __construct($color) {
$this->color = $color;
}
// Abstract methods
abstract public function calculateArea();
abstract public function getName();
// Regular method
public function getColor() {
return $this->color;
}
}
class Circle extends Shape {
private $radius;
public function __construct($color, $radius) {
parent::__construct($color);
$this->radius = $radius;
}
public function calculateArea() {
return 3.14 * $this->radius * $this->radius;
}
public function getName() {
return "Circle";
}
}
class Square extends Shape {
private $side;
public function __construct($color, $side) {
parent::__construct($color);
$this->side = $side;
}
public function calculateArea() {
return $this->side * $this->side;
}
public function getName() {
return "Square";
}
}
// Create shapes
$circle = new Circle("Red", 5);
echo $circle->getName() . " is " . $circle->getColor();
echo " with area: " . $circle->calculateArea();
$square = new Square("Blue", 4);
echo $square->getName() . " is " . $square->getColor();
echo " with area: " . $square->calculateArea();
?>
Output:
Circle is Red with area: 78.5
Square is Blue with area: 16
🔹 Real-World Example
Abstract classes are perfect for creating payment systems where different payment methods share common features but have unique processing logic. This ensures all payment types follow the same structure while implementing their specific requirements.
<?php
abstract class Payment {
protected $amount;
public function __construct($amount) {
$this->amount = $amount;
}
// Abstract method
abstract public function processPayment();
// Regular method
public function getAmount() {
return $this->amount;
}
}
class CreditCard extends Payment {
private $cardNumber;
public function __construct($amount, $cardNumber) {
parent::__construct($amount);
$this->cardNumber = $cardNumber;
}
public function processPayment() {
return "Processing $" . $this->amount .
" via Credit Card ending in " .
substr($this->cardNumber, -4);
}
}
class PayPal extends Payment {
private $email;
public function __construct($amount, $email) {
parent::__construct($amount);
$this->email = $email;
}
public function processPayment() {
return "Processing $" . $this->amount .
" via PayPal account: " . $this->email;
}
}
// Process payments
$cc = new CreditCard(100, "1234567890123456");
echo $cc->processPayment();
$pp = new PayPal(50, "[email protected]");
echo $pp->processPayment();
?>
Output:
Processing $100 via Credit Card ending in 3456
Processing $50 via PayPal account: [email protected]
💡 Key Points to Remember:
-
Use
abstractkeyword before class and methods - Cannot create objects from abstract classes
- Child classes must implement all abstract methods
- Can have both abstract and regular methods
- Can have properties and constructors
- Use when classes share common structure but different implementations