批改状态:合格
老师批语:
trait 可以当作类,但是不能被实例化,所以严格来讲不是类,
trait 就是
引入一些新功能,对我们没有任何影响(命名冲突除外),我们引入 trait 的当前类的优先级大于使用 use 引入的优先级,所以 trait 的功能可以看作是一种拓展。 重写没有任何影响,不重写也可以直接调用 trait 内的功能。
trait show{public static function getName (){return 'trait小明';}}//父类class One {public static function getName (){return 'One小明';}}//子类class Two extends One{use show;public static function getName (){return 'Two小明';}}echo Two::getName(); //Two小明
trait show{public static function getName (){return 'trait小明';}}//父类class One {public static function getName (){return 'One小明';}}//子类class Two extends One{use show;}echo Two::getName(); //trait小明
1.实例 解析 trait 新特性的功能;
简单一点理解就是我们可以把公共的方法写到 trait 中,这样可以节省大量重复代码,有点类似于 js 的封装
trait Copy{public static function getName(){echo 'trait小明';}}class CopyOne{use Copy;}class CopyTwo extends CopyOne{use Copy;}CopyTwo::getname(); //trait
trait FunOne{public static function sayOne(){echo 'hello 拓展One';}}trait FunTwo{public static function sayTwo(){echo 'hello 拓展Two';}}trait FunOneAndTwo{use FunOne,FunTwo;}class WorkOne{use FunOneAndTwo; //hello 拓展Two}WorkOne::sayTwo();
*如果命名冲突会爆出以下错误Trait method getName has not been applied, because there are collisions with other trait methods on reName in
这时候我们使用 trait 特性解决命名冲突问题
// 解决方案trait reNameOne{public static function getName(){echo 'nameONE';}}/****/trait reNameTwo{public static function getName(){echo 'nameTwo';}}/*** reNameOneAndTwo*/trait reNameOneAndTwo{use reNameOne,reNameTwo{reNameTwo::getName as nameOne;reNameOne::getName insteadOf reNameTwo; // 告诉引擎不要去reNameTwo中找getNmame,因为已经被替换了}}class reName{use reNameOneAndTwo;}reName::getName();
2.子类同名成员优先大于父类同名成员,如果子类,父类,trait 中存在同名方法的时候, 而 trait 在子类中调用,此时子类 > trait > 父类
// trait类trait show{// private static $name = 'trait小明';public static function getName (){return 'trait小明';}}//父类class One {// private static $name = 'One小明';public static function getName (){return 'One小明';}}//子类class Two extends One{use show;// private static $name = 'Two小明';public static function getName (){return 'Two小明';}}echo Two::getName(); //Two小明
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号