Skip to main content

PHP - Inheritance

With inheritance, a child class receives every public and protected property and method of its parent class, and it can add properties and methods of its own.

Note: A child class has no access to the private methods of its parent.

Use the extends keyword to create an inherited class.

In the next example, Strawberry extends Fruit. Thanks to inheritance, it can use the public $name and $color properties and the public __construct() and intro() methods of Fruit. On top of those, Strawberry defines one more method of its own, message().

Example

Strawberry gets everything public from Fruit and adds message():

Prompt
Create a PHP class Fruit with public properties $name and $color, a constructor, and an intro() method. Then create a Strawberry class that extends Fruit and adds a message() method. Create a Strawberry object and call both methods.
Output: PHP script

PHP - Inheritance and the Protected Access Modifier

The previous chapter showed that protected members are open to the class itself and to classes derived from it.

Outside code is a different story: a call to a protected method from there ends in an error, while public methods keep working normally.

Example

Here, outside code calls the protected method intro() and gets an error:

Prompt
Create a Fruit class with a protected intro() method and a Strawberry child class with a public message() method. Try calling intro() from outside the class to show it produces an error.
Output: PHP script

Example

The derived class can call the same protected method from inside, with no problem:

Prompt
Create a Fruit class with a protected intro() method and a Strawberry child class whose message() method calls the protected intro() from within the derived class.
Output: PHP script

PHP - Overriding Inherited Methods

A child class can replace a method it inherits. Just define a method with the same name in the child class; this is called overriding.

In the next example, Strawberry (the child) supplies its own __construct() and intro(), and these override the versions in Fruit (the parent):

Example

The versions of __construct() and intro() in Strawberry are the ones that run:

Prompt
Create a Fruit class with a constructor and intro() method. Then create a Strawberry child class that overrides both __construct() and intro() with its own implementations, adding a $weight property.
Output: PHP script

PHP - The final Keyword

Sometimes a class or method should stay exactly as written. The final keyword does that: on a class it blocks inheritance, and on a method it blocks overriding.

Example

A class marked final cannot be extended, so this code ends in a fatal error:

Prompt
Create a final class Fruit and then try to create a Strawberry class that extends it, which will result in an error.
Output: PHP script

Example

A method marked final cannot be overridden in a child class, so this code also ends in a fatal error:

Prompt
Create a Fruit class with a final intro() method, then create a Strawberry child class that tries to override the intro() method, which will result in an error.
Output: PHP script