trait 可以组合多个特性到一个类中,有点像汽车装配厂
<?php
trait BaseEat
{
public function eat()
{
echo 'eat' . PHP_EOL;
}
}
trait BaseSleep
{
public function sleep()
{
echo 'sleep' . PHP_EOL;
}
}
class Person
{
use BaseEat, BaseSleep;
}
$person = new Person();
$person->eat();
$person->sleep();
// eat
// sleep
参考
PHP之Trait详解