trait
is kind of solution to the famous
multiple inheritance
problem in PHP.
Using traits we can access methods from different traits that we use in our class
class Mobile {
public function battery() {
echo 'Battery: MB_06, MF_02, MF_00' . PHP_EOL;
}
}
trait Laser {
public function power() {
echo 'Power: 10 mW' . PHP_EOL;
}
}
trait Projector {
public function range() {
echo 'Range: 2 M' . PHP_EOL;
}
}
class Galaxy extends Mobile {
use Laser;
use Projector;
}
$obj1 = new Galaxy;
$obj1->range();
$obj1->power();
$obj1->battery();