PHP Interfaces and Traits Tutorial
PHP interfaces and traits tutorial with real-world examples
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
An interface defines what methods a class must implement but does not provide the actual implementation.
Think of an interface as a contract.
Contains only method signatures
No method body
A class must define all interface methods
Supports multiple interface inheritance
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");
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 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;
}
}
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);
A trait allows you to reuse methods across multiple classes.
Traits solve the problem of single inheritance in PHP.
Reusable code blocks
Contains methods (and properties)
Included using the use keyword
A class can use multiple traits
trait Greeting {
public function sayHello() {
echo "Hello from Trait!";
}
}
class User {
use Greeting;
}
$u = new User();
$u->sayHello();
trait Upload {
public function upload() {
echo "Uploading file...";
}
}
trait Download {
public function download() {
echo "Downloading file...";
}
}
class FileManager {
use Upload, Download;
}
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
Often, interfaces define "what must be done" while traits provide "how it can be done".
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!");
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");
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.