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

Tell me how can you pass a variable by reference?

952


Tell me how is it possible to know the number of rows returned in result set?

1090


How to declare an array in php?

1091


Is it more secure to use cookies to transfer session ids?

965


How big is varchar max?

977


What is difference between python and php?

968


Explain me what is x+ mode in fopen() used for?

1016


How many functions are there in php?

994


How do I run a php file?

965


Is php a float?

1014


Tools used for drawing er diagrams?

1000


Which is better mysql or sql?

1009


Is php a strongly typed language?

1007


Require_once(), require(), include(). What is difference between them?

980


Which is variable cost?

963