PHP Inheritance
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
UserControllershould never extendDatabaseHelper. Inject a service class instead; useextendsonly for genuine "is-a" relationships. - Weakening visibility in child overrides: Changing a
protectedparent method toprivatein a child class causes a fatal error. Overrides must stay equally visible or become more open (protectedtopublic). - 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.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.