0
0
PHPprogramming~10 mins

Interface vs abstract class vs trait in PHP - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Interface vs abstract class vs trait
Define Interface
Define Abstract Class
Define Trait
Use Interface in Class
Use Trait in Class
Create Object and Call Methods
Execution of Methods as per Definition
Shows how interfaces, abstract classes, and traits are defined and used in classes, then how objects call their methods.
Execution Sample
PHP
<?php
interface IExample {
  public function show();
}
abstract class AExample {
  abstract public function display();
}
trait TExample {
  public function traitMethod() {
    echo "Trait method called\n";
  }
}
class MyClass extends AExample implements IExample {
  use TExample;
  public function show() {
    echo "Interface method show() called\n";
  }
  public function display() {
    echo "Abstract method display() called\n";
  }
}
$obj = new MyClass();
$obj->show();
$obj->display();
$obj->traitMethod();
?>
Defines interface, abstract class, and trait; then a class uses all three and calls their methods.
Execution Table
StepActionEvaluationOutput
1Define interface IExample with method show()Interface created
2Define abstract class AExample with abstract method display()Abstract class created
3Define trait TExample with method traitMethod()Trait created
4Define class MyClass extending AExample, implementing IExample, using TExampleClass created with all features
5Create object $obj of MyClassObject instantiated
6Call $obj->show()Calls interface method implemented in MyClassInterface method show() called
7Call $obj->display()Calls abstract method implemented in MyClassAbstract method display() called
8Call $obj->traitMethod()Calls trait method used in MyClassTrait method called
💡 All methods called successfully, demonstrating interface, abstract class, and trait usage.
Variable Tracker
VariableStartAfter Object CreationAfter show()After display()After traitMethod()
$objundefinedInstance of MyClassInstance of MyClassInstance of MyClassInstance of MyClass
Key Moments - 3 Insights
Why can't we create an object directly from an interface or abstract class?
Interfaces and abstract classes are incomplete by design (see execution_table steps 1 and 2). They require a concrete class to implement or extend them before creating an object (step 4 and 5).
How does a trait differ from an interface or abstract class?
A trait provides reusable method code that a class can include directly (step 3 and 8), unlike interfaces which only declare methods, or abstract classes which can have abstract and concrete methods.
Can a class use multiple traits but only extend one abstract class?
Yes, PHP allows multiple traits to be used in a class but only one abstract class can be extended (demonstrated in step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when $obj->show() is called at step 6?
ATrait method called
BAbstract method display() called
CInterface method show() called
DError: method not found
💡 Hint
Check the Output column at step 6 in execution_table.
At which step is the trait method traitMethod() called?
AStep 5
BStep 8
CStep 7
DStep 6
💡 Hint
Look at the Action column and Output at step 8 in execution_table.
If MyClass did not implement the interface method show(), what would happen?
AFatal error: Class must implement interface method
BTrait method would be called instead
CCode runs normally
DAbstract method display() would be called automatically
💡 Hint
Interfaces require all methods to be implemented by the class (see key_moments about interfaces).
Concept Snapshot
Interface: declares methods without code; classes must implement all methods.
Abstract class: can have abstract and concrete methods; classes extend and implement abstract methods.
Trait: reusable method code included in classes; allows code sharing without inheritance.
Class can implement multiple interfaces, extend one abstract class, and use multiple traits.
Objects can only be created from concrete classes, not interfaces or abstract classes.
Full Transcript
This visual trace shows how PHP interfaces, abstract classes, and traits are defined and used. First, an interface IExample declares a method show(). Then, an abstract class AExample declares an abstract method display(). A trait TExample provides a concrete method traitMethod(). A class MyClass extends the abstract class, implements the interface, and uses the trait. When an object of MyClass is created, it can call all three methods: show() from the interface, display() from the abstract class, and traitMethod() from the trait. The execution table tracks each step, showing method calls and outputs. Key moments clarify why interfaces and abstract classes cannot be instantiated directly and how traits differ by providing reusable code. The quiz tests understanding of method calls and PHP rules about these constructs. This helps beginners see how these three PHP features work together in real code.