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 us how can we access the data sent through the url with the post method?
What is the use of namespace in php?
Can a super () and this () keywords be in same constructor?
What are the benefits of composer?
What can php do?
How to get a random value from a php array?
What is polymorphism?
What is php limit?
Which function is used to strip whitespace?
what is benefit of magento?
What is chrome logger?
How to retrieve values out of an array?
How to Retrieve a Cookie Value?
What is data abstraction in php?
How error handling is being handled by php?