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 is psr in php?
How to know user has read the email-php?
What are the different errors in php?
What is encapsulation in oop php?
How to get a total number of elements used in the array?
What is $globals php?
what are the errors and when we get that particular errors.and who will give that errors
What is the apache?
Explain how to execute a php script using command line.
How do you end a session in php?
What is the empty function?
write a program to print [123] [456] [789] note : braces also need to be printed