Loading…

What is traits? How it is used in php?

Traits is a group of 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 methods.

Example

                                        


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 oops interview questions

Ask Question