Inheritance

Inheritance lets one class build on another. In PHP the child declares the parent after extends, keeps what the parent allows and adds or replaces behavior of its own.

Extend a class

Use extends to reuse a base class in a child class. The child gains all public and protected members of the base.

Details are explained in the guide on inheritance with a longer Animal example.

<?php class Animal { public function voice(): string { return "Sound"; } } class Cat extends Animal { } $cat = new Cat(); echo $cat->voice(), PHP_EOL;

php extend.php

Sound

The empty child holds no method of its own and answers through the one it inherited. Inheritance copies behavior, not data: each object still has its own properties.

Call the parent

A child may override a method and still reuse the original logic. Call parent followed by two colons and the method name.

This pattern keeps shared logic in one place while allowing small changes in children.

<?php class Animal { public function voice(): string { return "Sound"; } } class Cat extends Animal { public function voice(): string { return parent::voice() . " Meow"; } } $cat = new Cat(); echo $cat->voice(), PHP_EOL;

php parent.php

Sound Meow

The child keeps its own line and puts the work of the parent in front of it. Without the parent call the original string would be lost, because the child method replaces the inherited one.

public protected private

Visibility controls who can read a property or call a method. Public members are open to all code.

Protected members stay inside the family of parent and children. Private members stay inside one class only.

<?php class Demo { public string $open = "yes"; protected string $family = "yes"; private string $secret = "yes"; public function reveal(): string { return $this->secret; } } class Child extends Demo { public function family(): string { return $this->family; } } $child = new Child(); echo $child->open, " ", $child->family(), " ", $child->reveal(), PHP_EOL;

php visibility.php

yes yes yes

Outside code reads the public property, the child method reads the protected one, and the private value leaves only through the public method of the class that declares it. A child that reads a private property of its parent sees an undefined property instead, because the private member is not inherited at all.

Abstract classes

An abstract class defines a contract and shares code but cannot be created directly.

Child classes extend it and fill in each abstract method with real logic.

<?php abstract class Shape { abstract public function area(): float; } class Square extends Shape { public function __construct(public float $side = 2.0) { } public function area(): float { return $this->side * $this->side; } } $square = new Square(3.0); echo $square->area(), PHP_EOL; try { $shape = new Shape(); } catch (Error $e) { echo $e->getMessage(), PHP_EOL; }

php abstract.php

9
Cannot instantiate abstract class Shape

The child fills in the method that the abstract class only names, so the square answers with its area. The abstract class itself refuses new, because a Shape without an area would be incomplete.

Exercises

  1. Add a Dog class that extends Animal and returns "Woof". Which method does it override?
  2. Give Cat a method that calls both its own voice() and the parent one, and print both results.
  3. Make reveal() protected and call it from outside the class. What does PHP do?
  4. Add a Rectangle class that extends Shape and takes a width and a height, then print the area of both shapes.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does extends give the child class?

    Answer

    The public and protected members of the parent

    The child inherits the public and protected members; private members stay inside the parent class.

  2. How does a child call the method it just overrode?

    Answer

    parent::method()

    The parent keyword with the double colon reaches the implementation of the class above.

  3. Which members can a child class read?

    Answer

    The public and protected ones

    A private property belongs to the class that declares it, so the child cannot see it.

  4. What is true of an abstract class?

    Answer

    It cannot be created with new

    An abstract class may hold real code too, but it is incomplete until a child fills in its abstract methods.

  5. What does this program print? <?php class Animal { public function voice(): string { return "Sound"; } } class Cat extends Animal { } $cat = new Cat(); echo $cat->voice();

    Answer

    Sound

    Cat inherits the method, so the call reaches the implementation in Animal.

  6. What does this program print? <?php class Animal { public function voice(): string { return "Sound"; } } class Cat extends Animal { public function voice(): string { return parent::voice() . " Meow"; } } $cat = new Cat(); echo $cat->voice();

    Answer

    Sound Meow

    The child builds on the parent string and appends its own word after it.

  7. What does this program print? <?php abstract class Shape { abstract public function area(): float; } try { $shape = new Shape(); } catch (Error $e) { echo $e->getMessage(); }

    Answer

    Cannot instantiate abstract class Shape

    An abstract class is incomplete, so the constructor of the object refuses to build it.

  8. What does this program print? <?php abstract class Shape { abstract public function area(): float; } class Square extends Shape { public function __construct(public float $side = 2.0) { } public function area(): float { return $this->side * $this->side; } } $square = new Square(3.0); echo $square->area();

    Answer

    9

    The area method multiplies the side by itself, and 3 times 3 is 9.

  9. What does this program print? <?php class Base { protected string $name = "base"; } class Child extends Base { public function name(): string { return $this->name; } } echo (new Child())->name();

    Answer

    base

    A protected property is part of the family, so the child method reads the value the parent declared.

  10. What does this program print? <?php class Animal { } class Cat extends Animal { } $cat = new Cat(); echo get_parent_class($cat);

    Answer

    Animal

    get_parent_class returns the name of the class the object extends.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev