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
Tell me how can you pass a variable by reference?
Tell me how is it possible to know the number of rows returned in result set?
How to declare an array in php?
Is it more secure to use cookies to transfer session ids?
How big is varchar max?
What is difference between python and php?
Explain me what is x+ mode in fopen() used for?
How many functions are there in php?
How do I run a php file?
Is php a float?
Tools used for drawing er diagrams?
Which is better mysql or sql?
Is php a strongly typed language?
Require_once(), require(), include(). What is difference between them?
Which is variable cost?