Traits in PHP are used for code re-use. Since PHP supports single inheritance, we cannot use methods fromĀ multiple classes directly in a single class.
Traits allow us to inject multiple methods inĀ a single class which can be accessed directly by the object of injected class.
A Trait is similar to a class but cannot be instantiated. The syntax of a trait is:
trait TraitName{ function fun1(){ ... } fucntion fun2(){ ... } } class SomeClass{ use TraitName; }
Here is an example:
<?php trait Car{ function wheels(){ echo "4 wheels"; } } trait Petrol{ function fuelType(){ echo "Petrol"; } } class Alto{ use Car, Petrol; function cost(){ echo "3 lakhs"; } } $alto = new Alto(); $alto->cost(); echo "<br/>"; $alto->fuelType(); echo "<br/>"; $alto->wheels();
Sir Very helpful docs……
please add how to develop HTTP API in wordpress