While testing for traits in PHP I was a bit confused why traits were introduced. I did some minor experiment. First I called trait methods directly in a class
<?php
trait HelloWorld {
public function sayHello() {
echo 'Hello World!';
}
}
class TheWorldIsNotEnough {
use HelloWorld;
public function sayHellos() {
$o = new HelloWorld();
$o->sayHello();
}
}
$o = new TheWorldIsNotEnough();
$o->sayHellos();
?>
I got an error
Fatal error: Cannot instantiate trait HelloWorld in C:\xampp\htdocs\test.php on line 35
But when I did this
<?php
trait HelloWorld {
public function sayHello() {
echo 'Hello World!';
}
}
class MyHelloWorld {
use HelloWorld;
}
class TheWorldIsNotEnough {
use HelloWorld;
public function sayHellos() {
$o = new MyHelloWorld();
$o->sayHello();
}
}
$o = new TheWorldIsNotEnough();
$o->sayHellos();
?>
i was able to call the trait method and the result displayed "Hello World! ". So what is advantage of using Traits and how is it different from abstract classes? Kindly help me understand the usage. Thanks.