PHP Interfaces and Traits Tutorial

11/22/2025
All Articles

PHP interfaces and traits tutorial with real-world examples

PHP Interfaces and Traits Tutorial

PHP Interfaces and Traits Tutorial: A Complete Beginner-Friendly Guide

Modern PHP applications rely heavily on interfaces and traits to write clean, reusable, and structured code. These features help you build large applications with better flexibility, maintainability, and scalability.

In this tutorial, you will learn:

  • What interfaces are

  • How to define and implement interfaces

  • Multiple interface implementation

  • What traits are

  • How to use traits to reuse code

  • How interfaces and traits work together

  • Real-world developer examples


What Is an Interface in PHP?

An interface defines what methods a class must implement but does not provide the actual implementation.

Think of an interface as a contract.

Key Points:

  • Contains only method signatures

  • No method body

  • A class must define all interface methods

  • Supports multiple interface inheritance

Example: Basic Interface

interface Logger {
    public function log($message);
}

class FileLogger implements Logger {
    public function log($message) {
        echo "Logging into file: $message";
    }
}

$log = new FileLogger();
$log->log("PHP Tutorial");

Multiple Interfaces in One Class

A class can implement multiple interfaces.

interface A {
    public function methodA();
}

interface B {
    public function methodB();
}

class Test implements A, B {
    public function methodA() { echo "A executed"; }
    public function methodB() { echo "B executed"; }
}

Interface with Type Hints (PHP 7+)

interface Shape {
    public function area(): float;
}

class Circle implements Shape {
    private $radius;

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

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

Real-World Example: Payment Gateway Interface

interface PaymentGateway {
    public function pay($amount);
}

class PayPal implements PaymentGateway {
    public function pay($amount) {
        echo "Paid Rs.$amount via PayPal";
    }
}

class Razorpay implements PaymentGateway {
    public function pay($amount) {
        echo "Paid Rs.$amount via Razorpay";
    }
}

function processPayment(PaymentGateway $gateway, $amount) {
    $gateway->pay($amount);
}

processPayment(new PayPal(), 500);

What Is a Trait in PHP?

A trait allows you to reuse methods across multiple classes.

Traits solve the problem of single inheritance in PHP.

Key Points:

  • Reusable code blocks

  • Contains methods (and properties)

  • Included using the use keyword

  • A class can use multiple traits

Example: Basic Trait

trait Greeting {
    public function sayHello() {
        echo "Hello from Trait!";
    }
}

class User {
    use Greeting;
}

$u = new User();
$u->sayHello();

Using Multiple Traits

trait Upload {
    public function upload() {
        echo "Uploading file...";
    }
}

trait Download {
    public function download() {
        echo "Downloading file...";
    }
}

class FileManager {
    use Upload, Download;
}

Method Conflict Resolution in Traits

If traits have methods with the same name, you must resolve manually.

    public function hello() { echo "Hello from A"; }
}

trait B {
    public function hello() { echo "Hello from B"; }
}

class Test {
    use A, B {
        B::hello insteadof A;
        A::hello as helloA;
    }
}

$t = new Test();
$t->hello();   // From B
$t->helloA();  // From A

Combining Interfaces and Traits

Often, interfaces define "what must be done" while traits provide "how it can be done".

Example:

interface Notifiable {
    public function notify($msg);
}

trait EmailNotification {
    public function sendEmail($msg) {
        echo "Email sent: $msg";
    }
}

class User implements Notifiable {
    use EmailNotification;

    public function notify($msg) {
        $this->sendEmail($msg);
    }
}

$user = new User();
$user->notify("Welcome to PHP!");

🔥 Real-World Example: Logging System

interface LogInterface {
    public function log($message);
}

trait TimestampTrait {
    public function addTimestamp($msg) {
        return date("Y-m-d H:i:s") . " - " . $msg;
    }
}

class FileLogger implements LogInterface {
    use TimestampTrait;

    public function log($message) {
        $msg = $this->addTimestamp($message);
        echo "Writing to file: $msg";
    }
}

$logger = new FileLogger();
$logger->log("System started");

Summary

In this tutorial, you learned:

  • Interfaces define required methods

  • Traits provide reusable code

  • A class can implement multiple interfaces

  • A class can use multiple traits

  • Interfaces + traits build clean and flexible architectures

These concepts are widely used in frameworks like Laravel, Symfony, and CodeIgniter.

Article