Lesson

PHP Inheritance

10 min reading
Free Course

PHP Inheritance: Extending Classes Safely with extends

PHP inheritance lets a child class reuse properties and methods from a parent class using the extends keyword. When specialized classes share common fields or behavior—like EmailNotification and SmsNotification extending Notification—inheritance removes repeated boilerplate.

Overriding Methods and Calling parent::

A child class inherits all public and protected members from its parent and can override any non-final method to customize its behavior. Use parent::methodName() inside an overridden method when you want to keep the parent's setup logic and add extra steps.

<?php
declare(strict_types=1);

class Notification
{
    public function __construct(protected string $recipient) {}

    public function send(string $message): string
    {
        return "Prepared notification for {$this->recipient}: {$message}";
    }
}

final class EmailNotification extends Notification
{
    public function __construct(string $recipient, private string $subject)
    {
        parent::__construct($recipient);
    }

    public function send(string $message): string
    {
        $base = parent::send($message);
        return "{$base} [Subject: {$this->subject}]";
    }
}

$mail = new EmailNotification('hana@example.com', 'Invoice Ready');
echo $mail->send('Your invoice #402 is attached.') . PHP_EOL;

Locking Classes with final and Favoring Composition

Marking a class or method final prevents subclasses from overriding it and breaking assumptions. Whenever a class needs behavior from several unrelated sources—such as logging plus caching—prefer composition (injecting helper services) over deep multi-level inheritance trees.

Common Mistakes to Avoid

  • Inheriting just to share utility methods: A UserController should never extend DatabaseHelper. Inject a service class instead; use extends only for genuine "is-a" relationships.
  • Weakening visibility in child overrides: Changing a protected parent method to private in a child class causes a fatal error. Overrides must stay equally visible or become more open (protected to public).
  • Changing required parameter signatures: Child methods must remain compatible with the parent method signature (Liskov Substitution Principle).

Frequently Asked Questions

Does PHP support multiple class inheritance?

No. A PHP class can extends only one parent class directly. To share methods across unrelated classes, use traits or implement multiple interfaces.

What does final class prevent?

Adding final before class blocks any other class from extending it, which is common for value objects and security-sensitive services.

Can a child class access private properties of its parent?

No. private members stay invisible even to child classes. Use protected or a protected getter method when subclasses need access.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
PHP Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum