Blogger Information
Blog 19
fans 0
comment 0
visits 16168
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php基础trait组合实现功能扩展、相同方法与替代、trait和interface接口进行组合
῀℡῀ᵒᵐᵍᵎᵎᵎ
Original
938 people have browsed it

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演示截图

课后总结

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

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:能理解就很好, 毕竟刚刚接触
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post