In the last tutorial, you learned how PHP Inheritance lets one class extend another. But inheritance alone has limits — a class can only extend one parent. PHP Interfaces, abstract classes, and traits solve different versions of this problem, giving you more flexible ways to structure related classes.

This is Tutorial 3 of 10 in our PHP Advanced series. You will learn what PHP Interfaces are, how abstract classes enforce structure, and how traits let you share code across unrelated classes.

What are PHP Interfaces?

PHP Interfaces define a contract that a class must follow. An interface lists method names a class must implement, but it never contains any actual code inside those methods. Any class that uses an interface must provide its own implementation for every listed method.

You define PHP Interfaces using the interface keyword, and a class agrees to follow one using the implements keyword.

<?php
interface Shape {
    public function calculateArea();
}

class Circle implements Shape {
    public $radius;

    public function __construct($radius) {
        $this->radius = $radius;
    }

    public function calculateArea() {
        return 3.14 * $this->radius * $this->radius;
    }
}
?>

Since Circle implements the Shape interface, it must include a calculateArea() method — leaving it out would trigger a fatal error, since PHP Interfaces enforce that every listed method actually exists.

Why Use Interfaces?

PHP Interfaces become powerful when multiple unrelated classes need to guarantee the same set of methods. A Circle and a Rectangle share nothing in common structurally, yet both can implement Shape and guarantee a calculateArea() method exists on each.

<?php
class Rectangle implements Shape {
    public $width;
    public $height;

    public function __construct($width, $height) {
        $this->width = $width;
        $this->height = $height;
    }

    public function calculateArea() {
        return $this->width * $this->height;
    }
}

$shapes = [new Circle(5), new Rectangle(4, 6)];

foreach ($shapes as $shape) {
    echo $shape->calculateArea() . "<br>";
}
?>

This loop works because every object in the array implements the same PHP Interfaces contract, regardless of how differently each class calculates its area internally.

What are Abstract Classes?

An abstract class sits somewhere between a regular class and an interface. It can contain fully working methods, but it can also define abstract methods that have no body — forcing any child class to implement them.

<?php
abstract class Vehicle {
    public $brand;

    public function __construct($brand) {
        $this->brand = $brand;
    }

    abstract public function startEngine();

    public function showBrand() {
        echo "Brand: " . $this->brand;
    }
}
?>

You cannot create an object directly from an abstract class — it only exists to be extended.

<?php
class Car extends Vehicle {
    public function startEngine() {
        echo "Car engine started.";
    }
}

$car1 = new Car("Toyota");
$car1->showBrand();
$car1->startEngine();
?>

Interfaces vs Abstract Classes

PHP Interfaces work best when unrelated classes need to guarantee the same methods with zero shared code. Abstract classes work best when related classes share some common logic but still need to implement certain methods differently themselves.

Feature Interface Abstract Class
Method bodies Never allowed Allowed for regular methods
Multiple inheritance A class can implement several A class can extend only one
Properties Not allowed Allowed

What are Traits?

Traits solve a different problem entirely. PHP only allows a class to extend one parent, but sometimes you want to share the exact same method across several unrelated classes without forcing them into an inheritance chain. Traits let you define reusable methods and pull them directly into any class using the use keyword.

<?php
trait Logger {
    public function logMessage($message) {
        echo "[LOG]: " . $message;
    }
}

class Order {
    use Logger;
}

class Payment {
    use Logger;
}

$order = new Order();
$order->logMessage("Order placed successfully.");
?>

Both Order and Payment gain access to logMessage() through the trait, even though neither class relates to the other through PHP Inheritance at all. For full technical documentation, refer to the official PHP traits manual.

Combining Multiple Traits

A single class can use more than one trait at the same time, separated by commas, giving you a flexible way to mix and match reusable behavior across your PHP Interfaces and classes.

<?php
trait Logger {
    public function logMessage($message) {
        echo "[LOG]: " . $message;
    }
}

trait Notifier {
    public function sendNotification($message) {
        echo "[NOTIFY]: " . $message;
    }
}

class Order {
    use Logger, Notifier;
}
?>

What’s Next?

You now understand PHP Interfaces, abstract classes, and traits — three different tools for structuring flexible, reusable object-oriented code. In the next tutorial, we will explore error handling using try/catch blocks and exceptions, essential for building reliable applications.

If you missed the previous lesson, check out Tutorial 2: PHP Inheritance Guide, or explore all lessons in our PHP category.

Practice Exercise

Complete the following tasks to reinforce what you learned in this tutorial:

  • Create a PHP file named interfaces-practice.php
  • Define an interface called Payable with a method called calculatePay()
  • Create two classes, Employee and Freelancer, that both implement Payable with different calculation logic
  • Create an abstract class called Appliance with an abstract method turnOn() and a regular method showing the brand
  • Create a child class that extends Appliance and implements turnOn() with its own message
  • Create a trait called Timestampable with a method that echoes the current date using the date() function
  • Use the Timestampable trait inside two unrelated classes and call the method from both

Bonus Challenge: Create an interface called Discountable with a method applyDiscount(), implement it in a Product class, and combine it with a trait called PriceFormatter that has a method to format a number with two decimal places.

About Author

Arshad Sultan

Arshad Sultan is the Founder of Tutorials Ocean and a Technical Architect & Full-Stack Developer with 15+ years of experience in PHP, Laravel, CRM development, API integrations, and AI-powered automation. He also runs Rapid Code, a software and AI automation agency, and has trained hundreds of students across Karachi's IT institutes since 2010.

Leave a Reply

Your email address will not be published. Required fields are marked *