博主信息
博文 32
粉丝 2
评论 0
访问量 37784
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
trait 的5种功能
简行
原创
1289人浏览过

1.代码复用

  1. <?php
  2. //trait功能:代码复用
  3. trait tindex
  4. {
  5. public function fun(){
  6. printf("<pre>%s</pre>",print_r(get_class_vars(__CLASS__), true));
  7. }
  8. // get_class_vars:返回由类的属性组成的数组
  9. }
  10. class Test1
  11. {
  12. protected $name ='鹰眼';
  13. protected $occ = '七武海';
  14. use tindex;
  15. }
  16. class Test2
  17. {
  18. protected $name ='红发';
  19. protected $occ = '海贼';
  20. use tindex;
  21. }
  22. echo (new Test1)->fun();
  23. echo "<hr>";
  24. echo (new Test2)->fun();

2.在继承上下文中的应用

  1. <?php
  2. //trait功能:在继承上下文中的应用
  3. trait tdemo
  4. {
  5. public static function fun(){
  6. return "这是trait中的fun方法";
  7. }
  8. }
  9. //父类/基类
  10. abstract class Dood
  11. {
  12. public static function fun(){
  13. return "这是基类中的fun方法";
  14. }
  15. }
  16. //子类
  17. class sonDood extends Dood
  18. {
  19. //引用trait
  20. use tdemo;
  21. public static function fun(){
  22. return "这是子类中的fun方法";
  23. }
  24. }
  25. // 子类,父类,trait中存在同名方法的时候, 而trait在子类中调用时,子类 > trait > 父类
  26. echo sonDood::fun();

3.实现功能扩展

  1. <?php
  2. // trait功能: 实现功能扩展
  3. trait good
  4. {
  5. public function getfun(){
  6. printf("<pre>%s</pre>",print_r(get_class_methods(__CLASS__), true));
  7. // get_class_methods:返回由类的方法名组成的数组
  8. }
  9. }
  10. trait user
  11. {
  12. protected $we = "下午好";
  13. public function get(){
  14. return $this->we;
  15. }
  16. }
  17. trait demo
  18. {
  19. use good;
  20. use user;
  21. }
  22. class cindex
  23. {
  24. use demo;
  25. }
  26. echo (new cindex)->get();
  27. echo "<hr>";
  28. echo (new cindex)->getfun();

4.解决命名冲突的问题

  1. <?php
  2. // trait功能:解决命名冲突的问题
  3. trait good
  4. {
  5. public function fun(){
  6. return "这是good中的fun()";
  7. }
  8. }
  9. trait good1
  10. {
  11. public function fun(){
  12. return "这是good1中的fun()";
  13. }
  14. }
  15. trait demo
  16. {
  17. use good,good1{
  18. //取别名
  19. good::fun as refun;
  20. //调用good1中的fun 替代good中fun
  21. good1::fun insteadOf good;
  22. }
  23. }
  24. class index
  25. {
  26. use demo;
  27. }
  28. echo (new index)->refun();
  29. echo "<hr>";
  30. echo (new index)->fun();

5.trait 和 interface接口的组合

  1. <?php
  2. // trait功能: trait 和 interface接口的组合
  3. //接口
  4. if(!interface_exists('inter')):
  5. interface inter
  6. {
  7. //抽象方法
  8. public function test();
  9. }
  10. endif;
  11. //trait
  12. if(!trait_exists('tra')){
  13. trait tra
  14. {
  15. protected $name ='trait';
  16. //实现
  17. public function test(){
  18. return "这是把原本在实现类中实现的抽象方法,改到{$this->name}中实现";
  19. }
  20. }
  21. }
  22. // 实现类
  23. class demo implements inter
  24. {
  25. use tra;
  26. }
  27. //客户端
  28. echo (new demo)->test();

批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:trait很灵活, 这里尽可能举出各种场景, 实际开发中可能并不会用到这么多的,但学习还是要多了解一些
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学