Home Quizzes Leaderboard Competitions Learn Hire Us
About Contact
Log In Sign Up
Learn PHP Inheritance & Interfaces

Inheritance & Interfaces

⏱ 20 min read read
Inheritance in PHP:

class Dog extends Animal {

// Dog inherits everything from Animal

}

parent:: --- Calling the Parent:

public function \_\_construct(string $name, int $age, string
$breed) {

parent::\_\_construct($name, $age); // call Animal's constructor

$this->breed = $breed;

}

PHP uses parent:: (not super() like Java/Python).

Method overriding works the same --- define same method name in
child.

PHP uses extends for inheritance (same keyword as Java).

PHP supports only SINGLE inheritance (one parent class).

Use interfaces for multiple type contracts.

Interfaces:

An interface defines a contract --- methods a class must implement. No
method bodies.

interface Shape {

public function area(): float;

public function perimeter(): float;

}

class Circle implements Shape {

public function \_\_construct(private float $radius) {}

public function area(): float { return M_PI * $this->radius **
2; }

public function perimeter(): float { return 2 * M_PI *
$this->radius; }

}

Abstract Classes:

Cannot be instantiated directly. May have abstract methods (no body)
that subclasses must implement.

abstract class Animal {

abstract public function makeSound(): string; // must implement

public function describe(): void { // can use directly

echo $this->makeSound();

}

}

final Keyword:

final class --- cannot be extended. final method --- cannot be
overridden.
Code Example
<?php

// Parent class

abstract class Animal {

public function \_\_construct(

protected string $name,

protected int $age

) {}

abstract public function makeSound(): string;

public function describe(): void {

echo "{$this->name} is {$this->age} years old.\n";

}

public function \_\_toString(): string {

return "{$this->name} ({$this->age} yrs)";

}

}

// Child class

class Dog extends Animal {

public function \_\_construct(string $name, int $age, private
string $breed) {

parent::\_\_construct($name, $age);

}

public function makeSound(): string {

return "{$this->name} barks: Woof!\n";

}

public function fetch(): void {

echo "{$this->name} fetches the ball!\n";

}

}

class Cat extends Animal {

public function makeSound(): string {

return "{$this->name} meows: Meow!\n";

}

}

$dog = new Dog("Rex", 3, "Labrador");

$cat = new Cat("Whiskers", 5);

$dog->describe();

echo $dog->makeSound();

$dog->fetch();

// Polymorphism

$animals = [$dog, $cat, new Dog("Buddy", 2, "Poodle")];

foreach ($animals as $a) {

echo $a->makeSound();

}

// instanceof

var_dump($dog instanceof Animal); // bool(true)

var_dump($dog instanceof Dog); // bool(true)

// Interface

interface Shape {

public function area(): float;

}

class Circle implements Shape {

public function \_\_construct(private float $radius) {}

public function area(): float { return M_PI * $this->radius **
2; }

}

echo number_format((new Circle(5))->area(), 2) . "\n"; // 78.54

?>
← Object-Oriented Programming --- Classes String Functions & Regular Expressions →

Log in to track your progress and earn badges as you complete lessons.

Log In to Track Progress