Answer Posted / zahid
In PHP, a trait is a way to enable developers to reuse methods of independent classes that exist in different inheritance hierarchies.
Simply put, traits allow you to create desirable methods in a class setting, using the trait keyword. You can then inherit this class through the use keyword
Example:
trait hello {
public function message1() {
echo "Nature is precious,
";
}
}
trait world{
public function message2() {
echo "So Let us unite to preserve it";
}
}
class InUnity {
use hello;
}
class WeCan {
use hello, world;
}
$obj = new InUnity();
$obj->message1();
echo "****************
";
$obj2 = new WeCan();
$obj2->message1();
$obj2->message2();
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers
What is difference between php and wordpress?
What is framework? How it works? What is advantage?
What is the use of hooks?
Explain mixed and callback functions?
Tell me how to retrieve a cookie value?
What is global array in php?
Which php mvc framework is best?
How to pass variables by references?
Explain me what is the meaning of a persistent cookie?
What is zend studio for?
What is php and how do you use it?
What is the main difference between php 4 and php 5?
How to get the value of current session id?
Which php framework is in demand?
How long is a php session valid?