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.
Interface vs abstract class vs trait in 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.
log method.<?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");
<?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();
sayHello method to the class without inheritance.<?php trait HelloTrait { public function sayHello() { echo "Hello!\n"; } } class Person { use HelloTrait; } $person = new Person(); $person->sayHello();
<?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();
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 // 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();
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.
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.