0
0
PHPprogramming~7 mins

Interface vs abstract class vs trait in PHP

Choose your learning style9 modes available
Introduction

Interfaces, abstract classes, and traits help organize code by defining how parts of a program should work together. They make code easier to reuse and change.

When you want to define a set of methods that different classes must implement (use interface).
When you want to share some common code and force some methods to be defined in child classes (use abstract class).
When you want to reuse code in many classes without using inheritance (use trait).
Syntax
PHP
<?php

// Interface example
interface LoggerInterface {
    public function log(string $message);
}

// Abstract class example
abstract class Animal {
    abstract public function makeSound();
    public function sleep() {
        echo "Sleeping...\n";
    }
}

// Trait example
trait HelloTrait {
    public function sayHello() {
        echo "Hello!\n";
    }
}

?>

An interface only declares method names without code.

An abstract class can have both method declarations and code.

A trait is a way to reuse code in multiple classes without inheritance.

Examples
Interface forces the class to implement the log method.
PHP
<?php
interface LoggerInterface {
    public function log(string $message);
}

class FileLogger implements LoggerInterface {
    public function log(string $message) {
        echo "Logging to file: $message\n";
    }
}

$logger = new FileLogger();
$logger->log("Test message");
Abstract class has a method with code and an abstract method to be defined in child.
PHP
<?php
abstract class Animal {
    abstract public function makeSound();
    public function sleep() {
        echo "Sleeping...\n";
    }
}

class Dog extends Animal {
    public function makeSound() {
        echo "Bark!\n";
    }
}

$dog = new Dog();
$dog->makeSound();
$dog->sleep();
Trait adds the sayHello method to the class without inheritance.
PHP
<?php
trait HelloTrait {
    public function sayHello() {
        echo "Hello!\n";
    }
}

class Person {
    use HelloTrait;
}

$person = new Person();
$person->sayHello();
Traits can be combined to add multiple methods to a class.
PHP
<?php
// Edge case: class uses multiple traits
trait A {
    public function methodA() {
        echo "Method A\n";
    }
}
trait B {
    public function methodB() {
        echo "Method B\n";
    }
}

class MultiTraitClass {
    use A, B;
}

$obj = new MultiTraitClass();
$obj->methodA();
$obj->methodB();
Sample Program

This program shows how interface, abstract class, and trait work together. The logger class must implement the interface method. The dog class extends an abstract class and uses a trait to add a method.

PHP
<?php
// Interface
interface LoggerInterface {
    public function log(string $message);
}

// Abstract class
abstract class Animal {
    abstract public function makeSound();
    public function sleep() {
        echo "Sleeping...\n";
    }
}

// Trait
trait HelloTrait {
    public function sayHello() {
        echo "Hello!\n";
    }
}

// Class implementing interface
class FileLogger implements LoggerInterface {
    public function log(string $message) {
        echo "Logging to file: $message\n";
    }
}

// Class extending abstract class and using trait
class Dog extends Animal {
    use HelloTrait;

    public function makeSound() {
        echo "Bark!\n";
    }
}

// Create objects
$logger = new FileLogger();
$dog = new Dog();

// Use interface method
$logger->log("Start logging");

// Use abstract class methods
$dog->makeSound();
$dog->sleep();

// Use trait method
$dog->sayHello();
OutputSuccess
Important Notes

Time complexity: These are design tools, so complexity depends on methods inside them.

Space complexity: No extra memory cost beyond normal class usage.

Common mistake: Trying to put code inside interface methods (not allowed).

Use interface to define a contract, abstract class to share code and force some methods, and trait to reuse code without inheritance.

Summary

Interfaces define method names without code, forcing classes to implement them.

Abstract classes can have both code and abstract methods to share common behavior.

Traits let you reuse code in many classes without using inheritance.