Interface vs traits in php

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The main difference between the Traits and Interfaces in PHP is that the Traits define the actual implementation of each method within each class, so many classes implement the same interface but having different behavior, while traits are just chunks of code injected in a class in PHP.

    Traits

    Traits are not interfaces at all. Traits can define both static members and static methods. It helps developers to reuse methods freely in several independent classes in different class hierarchies. Traits reduces the complexity, and avoids problems associated with multiple inheritance and Mixins. Note that PHP does not allow multiple inheritance. So Traits is used to fulfill this gap by allowing us to reuse same functionality in multiple classes.

    Syntax:

    trait namethis {

        function ReturnType() {  }

        function ReturnDescription() {  }

    }?>

    Traits can not implement interfaces. A trait allow both classes to use it for common interface requirement. It supports the use of abstract methods. It enables horizontal composition of behavior to traditional inheritance. Traits are a mechanism for code reuse in single inheritance languages such as PHP. Write the same code again, to avoid this use the traits. The traits are used when multiple classes share the same functionality.

    Example:

    trait HelloGeeks {

        public function geeks() {

            echo 'Hello World!';

        }

    }

    class Geeksforgeeks {

        use HelloGeeks;

        public function geeks() {

            echo 'Hello Geeks!';

        }

    }

    $obj = new Geeksforgeeks();

    $obj->geeks();

    ?>

    Output:

    Hello Geeks!

    Interface

    It specifies the lists of all such methods that a class must implement. Use the keyword Interface to implement interface same as a class. It can extend an interface using the extends operator. All the methods in Interface are abstract methods and can have their own constants. There is a concrete class concept which is a class that implements an interface which must implement all methods having the same names and signatures.
    All the methods in the interface must have a public access level.

    Syntax:

    interface MyInterface

    {

    }

    No two interface can be implemented by a particular class having same method name and signatures because it give error. Also helps in multiple inheritance because a class can implement more than one interface whereas it can extend only one class. Implementations can be changed without affecting the caller of the interface.

    Example:

    interface MyInterface{ 

        public function examplemethod1(); 

        public function examplemethod2(); 

    class MyClass implements MyInterface{ 

        public function examplemethod1(){ 

            echo "ExampleMethod1 Called" . "\n"

        

        public function examplemethod2(){ 

            echo "ExampleMethod2 Called". "\n"

        

    $ob = new MyClass; 

    $ob->examplemethod1(); 

    $ob->examplemethod2(); 

    ?> 

    Output:

    ExampleMethod1 Called
    ExampleMethod2 Called
    

    Is trait an interface?

    Traits are not interfaces at all. Traits can define both static members and static methods. It helps developers to reuse methods freely in several independent classes in different class hierarchies.

    Are traits like interfaces?

    Traits are interfaces Unlike interfaces in languages like Java, C# or Scala, new traits can be implemented for existing types (as with Hash above). That means abstractions can be created after-the-fact, and applied to existing libraries. Unlike inherent methods, trait methods are in scope only when their trait is.

    What is a trait in PHP?

    Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

    When should I use trait in PHP?

    Traits are used to declare methods that can be used in multiple classes. Traits can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).