Blogger Information
Blog 18
fans 1
comment 0
visits 17185
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示explain trait的新特性的功能
α清尘
Original
681 people have browsed it

实例演示explain trait的新特性的功能

自PHP5.4.0起,PHP实现了一种代码复用的方法,称之为Trait;Trait可以看做是类的实现部分,Trait为php单继承限制,提供了一套灵活的代码复用机制;

Trait的功能:

  • 实现代码的复用;
  • 实现扩展功能;
  • PHP提供了相应的trait命名冲突解决方案;
  • trait 与接口interface 组合使用;

trait 功能1:实现代码的复用 实例演示:

  1. // trait 功能1:实现代码的复用
  2. trait show{
  3. public function show(){
  4. printf('<pre>%s</pre>',print_r(get_class_vars(__CLASS__),true));
  5. }
  6. }
  7. class one{
  8. use show;
  9. protected $name="胡歌";
  10. protected $age="36";
  11. }
  12. $s=new one;
  13. echo $s->show();
  14. class two{
  15. use show;
  16. protected $name="山东";
  17. protected $age="26";
  18. }
  19. $s=new two;
  20. echo $s->show();

实现类的扩展功能 实例演示

  1. <?php
  2. // trait 实现类的扩展功能
  3. // 添加两个方法,打印所有属性,打印所有的方法
  4. trait demo1{
  5. public function show(){
  6. printf('<pre>%s</pre>',print_r(get_class_vars(__CLASS__),true));
  7. }
  8. }
  9. trait demo2{
  10. public function getMethod(){
  11. printf('<pre>%s</pre>',print_r(get_class_methods(__CLASS__),true));
  12. }
  13. }
  14. trait demo3{
  15. use demo1,demo2;
  16. }
  17. class one{
  18. use demo2;
  19. public $fruit="西瓜柿";
  20. public $prise="6元/斤";
  21. public function getInfo(){
  22. return $this->fruit.":".$this->prise;
  23. }
  24. }
  25. $a=new one();
  26. echo $a->getInfo();
  27. echo $a->getMethod();
  28. class two{
  29. use demo1;
  30. public $fruit="西瓜柿";
  31. public $prise="6元/斤";
  32. public function getInfo(){
  33. return $this->fruit.":".$this->prise;
  34. }
  35. }
  36. $a=new two();
  37. echo $a->getInfo();
  38. echo $a->show();
  39. ?>

trait 组合中命名冲突的解决方案 实例演示

  1. <?PHP
  2. // trait 组合中命名冲突的解决方案
  3. trait demo1{
  4. public function display(){
  5. return __METHOD__;
  6. }
  7. }
  8. trait demo2{
  9. public function display(){
  10. return __METHOD__;
  11. }
  12. }
  13. trait demo3{
  14. use demo2,demo1{
  15. // 给demo2起个别名d2
  16. demo2::display as d2;
  17. // 调用demo1::display替换掉demo2::display
  18. demo1::display insteadof demo2;
  19. }
  20. }
  21. class work{
  22. use demo3;
  23. }
  24. $work= new work();
  25. var_dump($work);
  26. echo "<br/>";
  27. echo $work->display();
  28. echo "<br/>";
  29. echo $work->d2();
  30. ?>

trait与interface组合

  1. <?PHP
  2. // trait 功能4 trait与interface组合
  3. interface iDemo{
  4. public static function index();
  5. }
  6. trait Demo{
  7. // 将接口方法中的抽象方法的实现过程交给trait来处理
  8. public static function index(){
  9. return __METHOD__;
  10. }
  11. }
  12. // 工作类
  13. class hello implements iDemo{
  14. use Demo;
  15. }
  16. // 客户端
  17. echo hello::index();
  18. ?>

Trait的优先级:

Trait,子类,父类中存在同名方法时,Trait在子类中调用,此时的优先级为:子类>trait>父类;

实例演示:

  1. <?php
  2. trait show{
  3. public static function getAge(){
  4. return __METHOD__;
  5. }
  6. }
  7. class One{
  8. public static function getAge(){
  9. return __METHOD__;
  10. }
  11. }
  12. class two extends One{
  13. use show;
  14. public static function getAge(){
  15. return __METHOD__;
  16. }
  17. }
  18. echo two::getAge();//two::getAge
  19. ?>
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!