Blogger Information
Blog 94
fans 0
comment 0
visits 92099
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP代码复用:trait。
可乐随笔
Original
1216 people have browsed it
PHP 实现了一种代码复用的方法,称为 trait。

Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制。Trait 为了减少单继承语言的限制,使开发人员能够自由地在不同层次结构内独立的类中复用 method。Trait 和 Class 组合的语义定义了一种减少复杂性的方式,避免传统多继承和 Mixin 类相关典型问题。

Trait 和 Class 相似,但仅仅旨在用细粒度和一致的方式来组合功能。 无法通过 trait 自身来实例化。它为传统继承增加了水平特性的组合;也就是说,应用的几个 Class 之间不需要继承。

trait实现了代码的复用

  1. <?php
  2. /*
  3. * trait实现了代码的复用
  4. * 并且突破了单继承的限制
  5. * trait是为不是类,不能实例化
  6. */
  7. trait Demo1
  8. {
  9. public function hello1()
  10. {
  11. return __METHOD__;
  12. }
  13. }
  14. trait Demo2
  15. {
  16. public function hello2()
  17. {
  18. return __METHOD__;
  19. }
  20. }
  21. class Demo
  22. {
  23. use Demo1,Demo2;
  24. public function hello()
  25. {
  26. return __METHOD__;
  27. }
  28. public function test1()
  29. {
  30. return $this->hell1();
  31. }
  32. public function test2()
  33. {
  34. return $this->hell2();
  35. }
  36. }
  37. $obj = new Demo;
  38. echo $obj->hello();
  39. echo '<hr>';
  40. echo $obj->test1();
  41. echo '<hr>';
  42. echo $obj->test2();

trait 优先级的问题

  1. <?php
  2. /*
  3. * trait 优先级的问题
  4. * 1. 当前类中的方法与train类,父类中的方法重名了,怎么办?
  5. * 2. 优先级: 当前类 > 引用的train类 > 父类
  6. */
  7. trait Demo1
  8. {
  9. public function hello()
  10. {
  11. return __METHOD__;
  12. }
  13. }
  14. trait Demo2
  15. {
  16. public function hello2()
  17. {
  18. return __METHOD__;
  19. }
  20. }
  21. class Test
  22. {
  23. public function hello()
  24. {
  25. return __METHOD__;
  26. }
  27. }
  28. class Demo extends Test
  29. {
  30. use Demo1,Demo2;
  31. public function hello()
  32. {
  33. return __METHOD__;
  34. }
  35. public function test1()
  36. {
  37. return $this->hell1();
  38. }
  39. public function test2()
  40. {
  41. return $this->hell2();
  42. }
  43. }
  44. $obj = new Demo;
  45. echo $obj->hello();
  46. //echo '<hr>';
  47. //echo $obj->test1();
  48. //echo '<hr>';
  49. //echo $obj->test2();

多个 train类中的方法重名了,怎么办

  1. <?php
  2. /*
  3. * trait 优生级的问题
  4. * 1. 当前类中的方法与train类,父类中的方法重名了,怎么办?
  5. * 2. 优先级: 当前类 > 引用的train类 > 父类
  6. * 3. 多个 train类中的方法重名了,怎么办?
  7. */
  8. trait Demo1
  9. {
  10. public function hello()
  11. {
  12. return __METHOD__;
  13. }
  14. }
  15. trait Demo2
  16. {
  17. public function hello()
  18. {
  19. return __METHOD__;
  20. }
  21. }
  22. class Test
  23. {
  24. public function hello()
  25. {
  26. return __METHOD__;
  27. }
  28. }
  29. class Demo extends Test
  30. {
  31. use Demo1,Demo2{
  32. Demo1::hello insteadof Demo2;
  33. Demo2::hello as Demo2Hello;
  34. }
  35. // public function hello()
  36. // {
  37. // return __METHOD__;
  38. // }
  39. public function test1()
  40. {
  41. return $this->hell1();
  42. }
  43. public function test2()
  44. {
  45. return $this->Demo2Hello();
  46. }
  47. }
  48. $obj = new Demo;
  49. echo $obj->hello();
  50. //echo '<hr>';
  51. //echo $obj->test1();
  52. echo '<hr>';
  53. echo $obj->test2();
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!