Extending Classes with Inheritance
Use extends to build class hierarchies and override methods.
Extending Classes with Inheritance is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro: PHP Inheritance
Inheritance lets a class (child) reuse and extend the code of another class (parent) using the extends keyword. PHP supports single inheritance.
Key Concepts
Key concepts:
- Child inherits all public/protected members
- Override parent methods to change behavior
- Call parent method with
parent:: - Use
finalto prevent overriding
Basic extends
Example:
<?php
class Shape {
public function __construct(protected string $color) {}
public function describe(): string { return "A {$this->color} shape"; }
}
class Circle extends Shape {
public function __construct(string $color, private float $radius) {
parent::__construct($color);
}
public function area(): float { return M_PI * $this->radius ** 2; }
}
$c = new Circle('red', 5);
echo $c->describe(); // A red shape
echo round($c->area()); // 79Method Overriding
Continued:
<?php
class Logger {
public function log(string $msg): void {
echo "[LOG] $msg" . PHP_EOL;
}
}
class DatabaseLogger extends Logger {
public function log(string $msg): void {
// Extend parent behavior
parent::log($msg);
// Also save to DB
echo "[DB] Saved: $msg" . PHP_EOL;
}
}
$l = new DatabaseLogger();
$l->log('User login');final Keyword
More depth:
<?php
class PaymentProcessor {
final public function processPayment(float $amount): void {
// Critical logic — cannot be overridden
$this->validate($amount);
$this->charge($amount);
$this->log($amount);
}
protected function validate(float $amount): void { /* ... */ }
protected function charge(float $amount): void { /* ... */ }
protected function log(float $amount): void { /* ... */ }
}instanceof Check
Practical use:
<?php
class Vehicle {}
class Car extends Vehicle {}
class ElectricCar extends Car {}
$ev = new ElectricCar();
var_dump($ev instanceof ElectricCar); // true
var_dump($ev instanceof Car); // true
var_dump($ev instanceof Vehicle); // true
// Also works with interfaces
var_dump($ev instanceof Countable); // falseAbstract Base Class
Advanced:
<?php
abstract class Notification {
abstract public function send(string $message): bool;
public function sendBatch(array $messages): void {
foreach ($messages as $msg) {
$this->send($msg);
}
}
}
class EmailNotification extends Notification {
public function send(string $message): bool {
echo "Email: $message" . PHP_EOL;
return true;
}
}Pattern
Common PHP Inheritance patterns are used to solve recurring design problems cleanly. Apply them consistently for readable, maintainable code.
When to Use
Knowing when to apply PHP Inheritance is as important as knowing how. Overuse leads to complexity; underuse leads to duplication.
Real-World Example
In real applications, PHP Inheritance appears frequently in frameworks, ORMs, and service layers. Understanding it prepares you to contribute to professional PHP projects.
Common Pitfalls
Avoid these mistakes with PHP Inheritance:
- Always call parent::__construct() to initialize parent state
- Don't override methods just to call parent with no changes
- Use final for security-critical methods
- Prefer composition over deep inheritance hierarchies
Quick Check
What keyword prevents a method from being overridden in a subclass?
Recap: PHP Inheritance
Summary:
- Always call parent::__construct() to initialize parent state
- Don't override methods just to call parent with no changes
- Use final for security-critical methods
- Prefer composition over deep inheritance hierarchies
Frequently asked questions
Is the “Extending Classes with Inheritance” lesson free?
Yes — the full text of “Extending Classes with Inheritance” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Extending Classes with Inheritance”?
Use extends to build class hierarchies and override methods. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Extending Classes with Inheritance” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Extending Classes with Inheritance
- Abstract Classes and Methods
- Implementing Interfaces
- Traits for Code Reuse