PHP
Abstract class inheritance
Learn to extend an abstract class and implement its abstract methods.
Inheriting an abstract class
You inherit an abstract class with extends, just like a normal class. But you must implement all abstract methods in the subclass. Otherwise PHP throws a fatal error and the subclass cannot be instantiated with new.
This creates a contract the abstract class imposes on the subclass: 'You will implement these methods yourself; inherit the rest from me'.
You can derive as many subclasses as you want from an abstract class. Each implements the abstract methods its own way; common behavior is shared, differences stay in subclasses.
Two subclasses, two implementations
Deriving multiple subclasses from the same abstract class is common. Each subclass implements the abstract methods its own way. One contract, many concrete behaviors.
For example, the Shape abstract class declares area(); Circle computes it from the radius, Rectangle from width and height. Both inherit the same describe() method.
$c = new Circle();
$r = new Rectangle();
echo $c->describe(); // Area: 3.14
echo $r->describe(); // Area: 20
Type variety converges in the abstract class; this is the basis of polymorphism.
Exercise Two subclasses: Circle and Rectangle
The Shape abstract class is provided below. Derive Circle and Rectangle classes from Shape and implement area() in each. Circle::area() returns 3.14, Rectangle::area() returns 20. Then create one instance of each and print the describe() outputs on separate lines: echo $c->describe() . "\n" . $r->describe() . "\n";
Show example solution
float returning 3.14 and 20 respectively. Create instances with new Circle() and new Rectangle(), print describe() output.
Example solution:
<?php
// Shape abstract class is provided
abstract class Shape {
abstract public function area(): float;
public function describe(): string {
return "Area: " . $this->area();
}
}
class Circle extends Shape {
public function area(): float {
return 3.14;
}
}
class Rectangle extends Shape {
public function area(): float {
return 20;
}
}
$c = new Circle();
$r = new Rectangle();
echo $c->describe() . "\n" . $r->describe() . "\n";
Test yourself
-
What happens if a subclass does not implement an abstract method?
- PHP throws a fatal error, the class cannot be used
- The subclass automatically becomes abstract but can still be instantiated with new
- The method returns a default value
- Nothing happens, the program runs normally
Show answer
PHP throws a fatal error, the class cannot be used — Correct. If the subclass does not implement an abstract method, PHP throws a fatal error; it cannot be instantiated.
-
Inherit the abstract class
class Circle ____ Shape {
public function area(): float {
return 3.14;
}
}
Show answer
class Circle extends Shape {
public function area(): float {
return 3.14;
}
}
-
Match the abstract inheritance concepts
- extends Shape
- Inherits the abstract class
- abstract method
- Must be implemented in subclass
- two subclasses
- Two different implementations
- describe()
- Concrete method, inherited
Next lesson: Template method pattern