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.
Practice
Solution
Step 1: Understand what an interface is in PHP
An interface only declares method names without any code, so classes must implement all methods.Step 2: Compare with traits and abstract classes
Traits provide reusable code, and abstract classes can have both code and abstract methods, unlike interfaces.Final Answer:
It defines method signatures without implementation, forcing classes to implement them. -> Option DQuick Check:
Interface = method signatures only [OK]
- Thinking interfaces can have method bodies
- Confusing traits with interfaces
- Believing interfaces can be instantiated
Solution
Step 1: Recall PHP syntax for traits
Traits are declared using the keywordtraitfollowed by the trait name and curly braces.Step 2: Eliminate incorrect options
abstract trait Logger {} is invalid because traits cannot be abstract. interface Logger {} declares an interface, and class Logger {} declares a class.Final Answer:
trait Logger {} -> Option CQuick Check:
Trait declaration uses 'trait' keyword [OK]
- Adding 'abstract' before trait keyword
- Confusing trait with interface or class syntax
- Using 'class' keyword for traits
trait Hello {
public function sayHello() {
return "Hello from trait!";
}
}
abstract class Base {
abstract public function sayHello();
}
class MyClass extends Base {
use Hello;
}
$obj = new MyClass();
echo $obj->sayHello();Solution
Step 1: Understand trait usage in class extending abstract
The traitHelloprovides the methodsayHello(). The abstract classBasedeclaressayHello()as abstract.Step 2: Check if MyClass implements abstract method
MyClass uses the trait which provides the required method, so no error occurs and the method returns the trait's string.Final Answer:
Hello from trait! -> Option AQuick Check:
Trait method satisfies abstract method [OK]
- Expecting fatal error for missing method
- Confusing trait method with abstract class method
- Thinking abstract method must be implemented in class only
interface Logger {
public function log(string $msg);
}
trait Logger {
public function log(string $msg) {
echo $msg;
}
}
class FileLogger implements Logger {
use Logger;
}Solution
Step 1: Check naming conflicts
Both the interface and trait are namedLogger, which causes a fatal error due to name collision.Step 2: Verify other rules
Classes can implement interfaces and use traits simultaneously. Traits can have parameters in methods. Interface methods cannot have implementation.Final Answer:
Trait and interface cannot have the same name. -> Option AQuick Check:
Name collision between trait and interface [OK]
- Thinking class can't use trait and implement interface
- Believing traits can't have parameters
- Assuming interface methods have code
connect() defined by an interface, share common code for disconnect() from an abstract class, and reuse logging code from a trait. Which is the best way to design this?Solution
Step 1: Understand roles of interface, abstract class, and trait
Interface defines required methods likeconnect(). Abstract class can provide shared code likedisconnect(). Trait is for reusable code like logging.Step 2: Combine them correctly in a class
PHP allows a class to implement interfaces, extend one abstract class, and use multiple traits. So the class should implement the interface, extend the abstract class, and use the trait.Final Answer:
Create an interface with connect(), an abstract class with disconnect(), a trait with logging, then a class that implements the interface, extends the abstract class, and uses the trait. -> Option BQuick Check:
Interface + abstract class + trait combined properly [OK]
- Trying to extend multiple classes
- Confusing trait and abstract class roles
- Extending interface instead of implementing
Ready to prove it?
Pass the test and this pattern moves unknown
