您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
php多个trait方法名冲突与insteadof
发布时间:2021-09-24 15:56:36编辑:雪饮阅读()
当一个派生类use了多个trait,那么这多个trait中若存在相同的方法名时候,就需要决定这个派生类在调用这个重复的方法时候到底是从那个trait中进行调用。
运行结果如:
declare(strict_types=1);
trait Dog{
public function say(){
echo "This is dog say";
}
public function walk(){
echo "This is dog walk";
}
}
trait Dog2{
public function say(){
echo "This is dog2 say";
}
public function walk(){
echo "This is dog2 walk";
}
}
class Animal{
public function eat(){
echo "This is animal eat";
}
}
class Cat extends Animal{
use Dog,Dog2{
Dog2::say insteadof Dog;
Dog2::walk insteadof Dog;
}
}
$cat = new Cat();
$cat->say();
$cat->walk();
?>
关键字词:php,trait,insteadof
上一篇:php多个trait用逗号相隔