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
How to remove duplicate values from a PHP Array?
What is a trait in php?
What is cookie and session in php?
Is nan in php?
How do you measure variables?
Which is incorrect with respect to separating php code and html?
Which function is used in php to check the data type of any variable?
What is a stored procedure in mysql?
What is the basic function to search files for lines (or other units of text) that contain a pattern.
What does sign mean php?
Explain the ternary conditional operator in php?
Explain what are the different errors in php?
How to remove white spaces from the beginning and/or the end of a string in php?
What does $_env mean?
Write a select query that will be displayed the duplicated site name and how many times it is duplicated?