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
What is mysqli_real_escape_string?
When do you use define() and when do you use const. What are the main differences between those two?
Can php run without server?
Does php have multiple inheritance?
Tell me whether it is possible to share a single instance of a memcache between multiple php projects?
Which Scripting Engine PHP uses?
How can we increase execution time of a php script?
Define metaphone()?
What are magic constants in php?
What is class extend in php?
What is php beginner?
How many types of arrays are there in php?
Why php is used with mysql?
What is purpose of @ in Php?
Why do you need php?