CodeBlanks Logo Icon CodeBlanks

Implementing with implements

PHP course · Intermediate · Lesson 48/115

Learn to implement an interface's methods in a class with implements.

The implements keyword

To declare that a class implements an interface, use the implements keyword: class Circle implements Shape { ... }.

The class must implement all methods declared in the interface with matching signatures. A missing method triggers a fatal error.

This binds the class to the interface's contract. From the outside, a Circle is a Shape and can be used wherever a Shape is expected.

A method implementation example

Let's write a Circle class that implements the Shape interface. First the interface: interface Shape { public function area(): float; }.

Then the class: class Circle implements Shape { public function area(): float { return 3.14 * $this->radius * $this->radius; } }. The class now defines area() with its own body.

From outside, $circle = new Circle() and calling $circle->area() runs Circle's own body. The contract is fulfilled.

Exercise Circle class implements the Shape interface

First define interface Shape { public function area(): float; }. Then write class Circle implements Shape with a private float $radius property and a __construct(float $radius) that runs $this->radius = $radius. The area() method does return 3.14 * $this->radius * $this->radius. Then create $circle = new Circle(5) and print echo $circle->area() . "\n".

Show example solution
float; }. Then inside class Circle implements Shape declare private float $radius, __construct and the area method.
Example solution:
<?php
// Define the Shape interface and implement Circle
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;
    }
}
$circle = new Circle(5);
echo $circle->area() . "\n";

Test yourself

  1. What is required when using implements?

    • Implement all of the interface's methods with matching signatures
    • Implement only one method of the interface
    • Make the methods private
    • Add properties to the interface
    Show answer

    Implement all of the interface's methods with matching signatures — Correct. implements requires all of the interface's methods with matching signatures.

  2. Add the implements keyword

    class Circle ____ Shape {
        public function area(): float { return 3.14; }
    }
    Show answer
    class Circle implements Shape {
        public function area(): float { return 3.14; }
    }
  3. Match the implements expressions

    implements Shape
    Class accepts the contract
    area() method
    Written with a body in the class
    missing method
    Triggers a fatal error
    interface Shape
    The contract declaration
  4. What happens if you don't write the interface's methods?

    • A warning is issued and the code runs
    • A fatal error is raised and the class is unusable
    • Methods are auto-created with empty bodies
    • Nothing happens, it is ignored
    Show answer

    A fatal error is raised and the class is unusable — Correct. implements requires all methods; a missing one raises a fatal error.

All lessons in Intermediate