What are traits? How is it used in PHP?

A trait is a group of various methods that reuse in single inheritance. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of processes. We can combine traits and classes to reduce complexity, avoid problems typically associated with Mixins and multiple inheritances.

Related Article: How to Use Traits in PHP



trait HelloWorld
     use Hello, World;

class MyWorld
     use HelloWorld;

$world = new MyWorld();
echo $world->sayHello() . " " . $world->sayWorld(); //Hello World

Suggest An Answer

No suggestions avaliable!

Latest post from PHP interview questions

Ask Question