<?php
/**
- trait实优先级
- 1.当前类中的方法与trait类,父类中的方法重名了,怎么办?
- 2.trait类优先级高于父类
- 3.当多个类中有同名的方法,怎么办?
*/
trait Demo1
{
public function hello()
{
return METHOD;
}
}
trait Demo2
{
public function hello()
{
return METHOD;
}
}
class Test{
public function hello()
{
return METHOD;
}
}
class Demo extends Test
{
use Demo1, Demo2{
//设置规则规定优先级
Demo1::hello insteadOf Demo2;
//函数名重复可以用别名访问
Demo2::hello as Demo2Hello;
}
// public function hello()
// {
// return METHOD;
// }
public function test1()
{
return this->Demo2Hello();
}
}
obj->hello();
// echo "
";
// echo obj->test2();
网友评论