What are traits? How is it used in php?
Answer / 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 |
What are the advantages of stored procedures, triggers, indexes in php?
What is php addslashes?
how can i upload only pdf files in my website? files has not a pdf extension mantioned
What are Routines?
What are the differences between session and cookie?
How to read a file in binary mode?
difference between clanguage and c++
What does nan stand for computer science?
How do you put a space in html?
What is action hooks and filter hooks?
Write a statement to show the joining of multiple comparisons in php?
What are the rules to determine the “truth” of any value which is not already of the boolean type?