博主信息
博文 19
粉丝 0
评论 0
访问量 22108
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
php基础trait组合实现功能扩展、相同方法与替代、trait和interface接口进行组合
῀℡῀ᵒᵐᵍᵎᵎᵎ
原创
1198人浏览过

1 trait组合实现功能扩展

1.1代码演示

  1. <?php
  2. //trait组合实现功能扩展
  3. trait tTime
  4. {
  5. protected function getTime()
  6. {
  7. //获取当前日期时间并返回
  8. return date("Y-m-d");
  9. }
  10. }
  11. trait tWork
  12. {
  13. protected $worker = '学习trait';
  14. protected function getWork()
  15. {
  16. //获取当前工作并返回
  17. return $this->worker;
  18. }
  19. }
  20. trait tToday
  21. {
  22. //引入trait
  23. use tTime, tWork;
  24. protected $name = '小周';
  25. protected $day = '1';
  26. }
  27. class Today
  28. {
  29. use tToday;
  30. public $ku = '其它什么都没做';
  31. public function say()
  32. {
  33. //返回创建与引入的值
  34. return $this->name . '在' . $this->getTime() . '花费' . $this->day . '天时间来' . $this->getWork(). ',' . $this->ku;
  35. }
  36. }
  37. //客户端代码
  38. $work = new Today();
  39. echo $work->say();

1.2 演示截图

1.3 trait中相同方法与替代

1.3.1代码演示

  1. <?php
  2. //trait中相同方法与替代
  3. trait tTime
  4. {
  5. protected function getTime()
  6. {
  7. //获取当前日期时间并返回
  8. return '现在的时间是'.date("Y-m-d H:i:s");
  9. }
  10. }
  11. trait tWork
  12. {
  13. protected $worker = '学习trait';
  14. protected function getWork()
  15. {
  16. //获取当前工作并返回
  17. return $this->worker;
  18. }
  19. }
  20. trait tWorks
  21. {
  22. protected $workers = '玩耍';
  23. protected function getWork()
  24. {
  25. //获取当前工作并返回
  26. return $this->workers;
  27. }
  28. }
  29. trait tToday
  30. {
  31. //引入trait
  32. use tTime, tWork, tWorks {
  33. //1.替代
  34. tWork::getWork insteadof tWorks;
  35. //2.别名
  36. tWorks::getWork as tTodays;
  37. }
  38. //as:还可以修改trait成员的访问控制
  39. use tWork {getWork as public tWorkss;}
  40. protected $name = '小周';
  41. protected $day = '1';
  42. }
  43. class Today
  44. {
  45. use tToday;
  46. public $ku = '其它什么都没做';
  47. public function say()
  48. {
  49. //返回创建与引入的值
  50. return $this->name . '在' . $this->getTime() . '花费' . $this->day . '天时间来' . $this->tTodays(). ',' . $this->ku;
  51. }
  52. }
  53. //客户端代码
  54. $work = new Today();
  55. echo $work->say();
  56. echo '<br>';
  57. echo $work->tWorkss();

1.3.2演示截图

1.4 trait和interface接口进行组合

1.4.1代码演示

  1. <?php
  2. //trait :trait和interface接口进行组合
  3. if (!interface_exists ('iTime')){
  4. interface iTime
  5. {
  6. public static function index();
  7. }
  8. }
  9. if (!trait_exists('tTime')){
  10. trait tTime
  11. {
  12. public static function index()
  13. {
  14. return '现在的时间是'.date("Y-m-d H:i:s");
  15. }
  16. }
  17. }
  18. //实现这个类
  19. if (!class_exists('Work')){
  20. class Work implements iTime
  21. {
  22. use tTime;
  23. }
  24. }
  25. echo Work::index();

1.4.2演示截图

课后总结

理解是能理解,也想搞一个实例,但是现在头脑里没有那个概念

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

批改状态:合格

老师批语:能理解就很好, 毕竟刚刚接触
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系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+教程免费学