Lesson 3 of 7
Inheritance - extends, parent:: & Method Overriding
Inheritance lets one class extend another, inheriting its properties and methods. PHP supports single inheritance - one parent class. Method overriding allows a subclass to provide a specialised implementation. The parent:: keyword calls the overridden parent method - useful for extending rather than replacing behaviour.
Inheritance in Practice
<?php
declare(strict_types=1);
// ── Base class with shared behaviour ──────────────────────────
abstract class Content {
private static int $nextId = 1;
public readonly int $id;
public readonly DateTimeImmutable $createdAt;
public function __construct(
protected string $title,
protected string $slug,
protected bool $published = false,
) {
$this->id = self::$nextId++;
$this->createdAt = new DateTimeImmutable("now", new DateTimeZone("UTC"));
}
// ── Template method - defines algorithm, subclasses fill steps ──
final public function render(): string {
return sprintf(
'<article id="%s">%s%s</article>',
htmlspecialchars($this->slug),
$this->renderHeader(),
$this->renderBody()
);
}
protected function renderHeader(): string {
return '<h1>' . htmlspecialchars($this->title) . '</h1>';
}
abstract protected function renderBody(): string; // subclass MUST implement
public function getTitle(): string { return $this->title; }
public function getSlug(): string { return $this->slug; }
public function isPublished(): bool { return $this->published; }
}
// ── Concrete subclass ──────────────────────────────────────────
class BlogPost extends Content {
public function __construct(
string $title,
string $slug,
private string $body,
private string $author,
private array $tags = [],
bool $published = false,
) {
parent::__construct($title, $slug, $published); // call parent constructor
}
// ── Override parent method - extend behaviour ─────────────
protected function renderHeader(): string {
$base = parent::renderHeader(); // get parent's header
return $base . '<p class="author">By ' . htmlspecialchars($this->author) . '</p>';
}
protected function renderBody(): string {
return '<div class="body">' . nl2br(htmlspecialchars($this->body)) . '</div>';
}
public function getAuthor(): string { return $this->author; }
public function getTags(): array { return $this->tags; }
}
class Page extends Content {
public function __construct(
string $title,
string $slug,
private string $html, // trusted admin HTML content
) {
parent::__construct($title, $slug, true); // pages always published
}
protected function renderBody(): string {
return '<div class="page-content">' . $this->html . '</div>';
}
}
// ── Polymorphism - same interface, different behaviour ────────
$items = [
new BlogPost("PHP Basics", "php-basics", "Great language!", "Alice", ["php"]),
new Page("About Us", "about", "<p>We love PHP.</p>"),
];
foreach ($items as $item) {
echo $item->render() . "
"; // each renders differently
}
// ── instanceof ────────────────────────────────────────────────
var_dump($items[0] instanceof BlogPost); // true
var_dump($items[0] instanceof Content); // true (parent)
var_dump($items[0] instanceof Page); // falseQuick Quiz
Tip
Tip
Practice Inheritance extends parent Method Overriding in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Inheritance - extends, parent:: & Method Overriding
Practice Task
Note
Practice Task - (1) Write a working example of Inheritance extends parent Method Overriding from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Inheritance extends parent Method Overriding is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.
Finished this lesson?