Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

What are traits? How is it used in php?

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


Please Help Members By Posting Answers For Below Questions

Which function would you use to determine the length of a string in php?

1096


Why do we use csrf token?

995


How to remove html tags from data in php?

1067


How to create a session? How to remove data from a session?

1039


Tell me what is the use of isset() in php?

1125


How to check curl is enabled or not in PHP

1177


Explain about PHP filter and why it should be used?

983


What is singleton pattern in php?

948


What is the difference between client-side and server-side programming?

1174


How to include variables in double-quoted strings in php?

1010


What are the uses of explode() and implode() functions?

1169


What does mvc stand for and what does each component do?

1158


What is variable give example?

1156


How do sessions work in php?

1040


How can you compare objects in php?

1001