CodeBlanks Logo Icon CodeBlanks

Polymorphism

PHP course · Intermediate · Lesson 50/115

Learn to use different classes implementing the same interface as a common type.

Polymorphism

Polymorphism lets different classes that implement the same interface be used as a common type. When a Shape interface is implemented by both Circle and Square, each is a Shape.

A function can take the interface as a parameter: function describe(Shape $shape). Inside it calls $shape->area(); whether you pass a Circle or a Square, the same call works.

So code depends on the contract, not the concrete class. Adding a new Shape class doesn't change the describe function. This is the basis of open-extensible design.

An interface-typed parameter

When a function parameter is typed as an interface, you can pass any class implementing it. Example: function describe(Shape $shape): void { echo $shape->area(); }.

Now both describe(new Circle(2)) and describe(new Square(3)) are valid. The function doesn't know the concrete class; it only trusts that area() exists.

PHP checks whether the passed object implements the interface. If not, a fatal error is raised. This provides type safety.

Exercise A polymorphism example with Shape

First define interface Shape { public function area(): float; }. Then write class Circle implements Shape and class Square implements Shape. Each has a private float property (Circle: $radius, Square: $side), a __construct(float) and an area() method. Circle returns 3.14 * $this->radius * $this->radius, Square returns $this->side * $this->side. Then write function describe(Shape $shape): void { echo $shape->area() . "\n"; } and call describe(new Circle(2)) and describe(new Square(3)).

Show example solution
First define interface Shape. Write Circle and Square with implements Shape, each with a property + __construct + area(). Then write describe(Shape $shape) and call it with two objects.
Example solution:
<?php
// Shape interface and polymorphism example
interface Shape {
    public function area(): float;
}
class Circle implements Shape {
    private float $radius;
    public function __construct(float $radius) {
        $this->radius = $radius;
    }
    public function area(): float {
        return 3.14 * $this->radius * $this->radius;
    }
}
class Square implements Shape {
    private float $side;
    public function __construct(float $side) {
        $this->side = $side;
    }
    public function area(): float {
        return $this->side * $this->side;
    }
}
function describe(Shape $shape): void {
    echo $shape->area() . "\n";
}
describe(new Circle(2));
describe(new Square(3));

Test yourself

  1. What does polymorphism enable?

    • A class having multiple parents
    • Using classes that implement the same interface as a common type
    • Hiding properties
    • Making methods private
    Show answer

    Using classes that implement the same interface as a common type — Correct. Polymorphism lets you use classes implementing the same interface as a common type.

  2. Write an interface-typed parameter

    function describe(____ $shape) {
        echo $shape->area();
    }
    Show answer
    function describe(Shape $shape) {
        echo $shape->area();
    }
  3. Match the polymorphism expressions

    interface type
    Common parameter
    Circle, Square
    Implement the same contract
    describe($shape)
    Accepts both
    $shape->area()
    Single-form call
  4. What is true for classes implementing the same interface?

    • They cannot replace each other
    • They can only be used as parent classes
    • They can be used commonly via an interface-typed parameter
    • Their method names must differ
    Show answer

    They can be used commonly via an interface-typed parameter — Correct. Classes implementing the same interface are used commonly via an interface-typed parameter.

All lessons in Intermediate