quick links

What is PHP Traits?

Traits is look like of class. it contain group of methods you want to use in another class.

Like abstract class we can't create object of traits but we can use it in another class.

So it's solve multiple inheritance problem in PHP.

It's lot of look like interface, but diffrent is in interface we can only define method/function but we can't implement but in traits we can do it.

<?php
trait Hello
{
function sayHello() {
echo "Hello";
}
}
trait World
{
function sayWorld() {
echo "World";
}
}
class MyWorld
{
use Hello, World;
}
$world = new MyWorld();
echo $world->sayHello() . " " . $world->sayWorld(); 
?>​

OUTPUT: Hello World

 

About Us

Joomla Web Solution

We love working with PHP, HTML5, CSS3, jQuery and the most-loved content management framework in the world, Joomla!

LIKE US ON FACEBOOK