[麦先生]PHP5.4以上更新的伪"多继承"--trait语法
trait PHP
{
public function pcode()
{
echo '我可以编写PHP代码';
}
}
trait Java
{
public function jcode()
{
echo '我可以编写JAVA代码';
}
}
class Stu
{
use PHP , Java;
}
$stu = new Stu;
$stu->pcode(); //输出结果我可以编写PHP代码
$stu->jcode(); //输出的结果是我可以编写PHP代码 我可以编写JAVA代码; 实现了所谓的多继承
但是需要注意的是如果两个方法名相同就会出现报错: "Trait method can has not been applied, because there are collisions with other trait methods on App\User";
解决方案:在User.php下起别名
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable,EntrustUserTrait, CanResetPassword{
EntrustUserTrait::can as may;
Authorizable::can insteadof EntrustUserTrait;
}