Home » PHP » What is the difference between interface and traits?

What is the difference between interface and traits?

The following differences are given below-

  1. An Interface can not provide a default implementation of a method body, while a trait can.
  2. An Interface is a polymorphic, inherited construct – while a trait is not.
  3. Multiple Interfaces can be used in the same class, and so can multiple traits.
interface MyInterface{  
    public function A(); 
    public function B();  
class MyClass implements MyInterface{ 
    public function A(){ 
        echo "A Called" . "\n"; 
    public function B(){ 
        echo "B Called". "\n"; 
$obj = new MyClass; 

Output -> A Called
          B Called
trait A {
    public function A() {
        echo 'A ';

trait B {
    public function B() {
        echo 'B';

class AB {
    use A, B;
    public function C() {
        echo '!';

$o = new AB();

Output -> A B!

Check Also

What is the Polymorphism in PHP?

Polymorphism is one of the PHP Object Oriented Programming (OOP) features. Polymorphism is derived from …

Leave a Reply

Your email address will not be published. Required fields are marked *