What is Traits and how does it work?

Traits are started from version 5.4 in PHP and it is a new feature of PHP. Trait is one kind of Mixen in PHP as it allows to mix classes with into an existing class. It is simply a group of method that we want to include with another class. A trait is similar to abstract and interface in this way that, here you can’t instantiate or create object directly.

We can use more traits in a single class. The following code shows practical example.

More traits in single Class
Even we can use single trait on multiple classes. See the following code where we use single trait on multiple classes.

Single trait on multiple classes



Baca juga

Post a Comment