Menu

PHP Inheritance: extends, parent:: and Overriding

In PHP a class inherits from another with class Dog extends Animal: it gets the parent's public and protected properties and methods, can override them, and can still call the original with parent::method(). Learn overriding, protected, final, instanceof and the private method trap.

This page includes runnable editors - edit, run, and see output instantly.

In PHP a class inherits from another with extends: class Dog extends Animal gives Dog every public and protected property and method of Animal. The child can add new methods, override inherited ones by declaring a method with the same name, and call the original with parent::method().

Dog has no constructor, so it uses Animal's, which is why new Dog('Rex') works. A child class can only extend one parent; PHP has no multiple inheritance of classes.

Call the parent method with parent::

Overriding replaces the parent's method completely. To extend it instead of replacing it, call parent::method() from the child and build on its result:

The same works for constructors with parent::__construct(...), which the constructor page covers, including what happens when you forget it.

protected vs private in child classes

A protected member is hidden from outside code but visible to every class in the family. A private member is visible only in the class that declares it, so a child class cannot see it at all:

isset($this->pin) is false in the child because the child has no access to the parent's private $pin, while the parent's own method hasPin() still reads it. Use protected for what subclasses are meant to build on, and private for details you want to keep free to change.

Polymorphism and instanceof

Code written against the parent type works with every child. Each object runs its own version of an overridden method, so one loop handles all of them. instanceof checks whether an object is of a class or any of its children:

When the parent should never be used on its own (a plain Shape with area 0 makes little sense), make it an abstract class and declare area() abstract.

Overriding rules: signatures must stay compatible

An overriding method must accept at least what the parent accepted and return something the parent's callers can use. You can add an optional parameter, widen a parameter type, or narrow a return type; you cannot add a required parameter or make a public method private.

Change string $sep = ',' to a required string $sep and PHP refuses to compile the class with a fatal error ("Declaration of CsvExporter::export(array rows,stringrows, string sep): string must be compatible with Exporter::export(array $rows): string").

final classes and methods

final stops inheritance at that point. A final method cannot be overridden, and a final class cannot be extended. Both are checked when the file is compiled, so the script does not start at all:

<?php
class Payment
{
    final public function charge(int $cents): string
    {
        return "charged $cents";
    }
}

class FreePayment extends Payment
{
    public function charge(int $cents): string
    {
        return 'free';
    }
}
PHP Fatal error:  Cannot override final method Payment::charge() in /home/index.php on line 12

For a final class the message is Class B cannot extend final class A. Mark a method final when the parent relies on it behaving exactly one way, for example a method that checks permissions before doing anything else.

The private method trap

A private method is not inherited, so a child method with the same name does not override it. Code in the parent keeps calling its own private version, which surprises people who expect the child's method to run:

greet() still prints Hello: the parent's $this->word() resolves to the parent's private method. Change private to protected in Greeter and run it again; now the child's method is a real override and greet() says Bonjour.

Frequently Asked Questions

How does extends work in PHP?

class Dog extends Animal makes Dog a child of Animal. A Dog object has every public and protected property and method of Animal, can add its own, and can replace (override) inherited methods by declaring a method with the same name.

How do I call the parent method in PHP?

Use parent::methodName() inside the child's method, for example return parent::describe() . ' and more';. For the constructor it is parent::__construct(...).

Does PHP support multiple inheritance?

No. A class can extend only one parent class. To share behavior from several sources, implement several interfaces (class A implements B, C) and use traits (use Loggable, Timestamps;).

What does final mean in PHP?

A final method cannot be overridden by a child class, and a final class cannot be extended at all. Breaking either rule is a fatal error when the class is compiled, for example Cannot override final method A::f().

Can a child class access a private property of its parent in PHP?

No. A private member belongs only to the class that declares it, so in the child $this->pin is an undefined property (a warning, then null) and a child method with the same name does not override a private one. Declare the member protected when child classes need it.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED