Blogger Information
Blog 32
fans 0
comment 0
visits 27416
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP中接口与trait的用法
Yang_Sir
Original
724 people have browsed it

接口定义类中需要实现的方法,制定统一的规范
一个类可以实现多个接口
在不同层次中都可以使用trait中的方法,以弥补单继承语言的缺陷

1.接口

  • 接口中定义常量和抽象方法
  • 接口直接可以继承,类可以继承多个接口
  • 在抽象方法中可以先只实现部分接口的中的方法,但是在继承该抽象方法的常规类中必须实现接口中的所有方法
  1. <?php
  2. //创建文本处理接口,有读,写功能
  3. interface iText1
  4. {
  5. const FORMAT = 'Y-m-d H:i:s';//接口常量:时间格式
  6. public function open($filename,$type);//抽象方法
  7. }
  8. //接口继承
  9. interface iText2 extends iText1
  10. {
  11. public function read();
  12. public function write($text);
  13. }
  14. //抽象类实现打开/创建文件功能:open();
  15. abstract class aText implements iText1,iText2
  16. {
  17. public $file = null;//保存文件链接句柄
  18. public function open($filename,$type){
  19. $this->file = fopen($filename,$type);
  20. }
  21. }
  22. //常规类实现其它功能
  23. class text extends aText
  24. {
  25. //向文件写入内容
  26. public function write($text){
  27. $str = date(self::FORMAT,time()).':'.$text;
  28. fwrite($this->file,$str);
  29. }
  30. //读取文件内容
  31. public function read(){
  32. while(!feof($this->file)){
  33. echo fgets($this->file),'<br>';
  34. }
  35. }
  36. //关闭文件链接
  37. public function __destruct()
  38. {
  39. fclose($this->file);
  40. }
  41. }
  42. // $text = new text();
  43. // $text->open('text/test.txt','w+');//创建了一个txt文件,以读写方式打开
  44. // $text->write("写入第一条1\r\n");
  45. // $text->read();//无内容
  46. // $text->open('text/test.txt','r');
  47. // $text->read();//输出:2020-05-02 19:12:09:写入第一条1

2.trait

  • trait与类一样,可以定义变量和方法
  • 在类中通过use关键字引入trait中的变量和方法,如同复制了代码
  1. //trait 代码复用
  2. trait tDemo
  3. {
  4. //格式化数组输出
  5. public static function myPrint(array $arr){
  6. return sprintf("<pre>%s</pre>",print_r($arr,true));
  7. }
  8. public static function write(){
  9. echo '我是trait中的write方法';
  10. }
  11. }
  12. class Demo1
  13. {
  14. use tDemo;//使用use关键字复制trait中的代码
  15. public static function write(){
  16. echo '我是Demo1中的write方法';
  17. }
  18. }
  19. echo Demo1::myPrint([1,4,5]);
  20. //输出Array
  21. // (
  22. // [0] => 1
  23. // [1] => 4
  24. // [2] => 5
  25. // )
  26. class Demo2 extends Demo1
  27. {
  28. use tDemo;
  29. // public static function write(){
  30. // echo '我是Demo2中的write方法';
  31. // }
  32. }
  33. echo Demo2::myPrint([5,5,5]);
  34. //输出Array
  35. // Array
  36. // (
  37. // [0] => 5
  38. // [1] => 5
  39. // [2] => 5
  40. // )
  41. Demo2::write();//输出:我是trait中的write方法
  42. //在类继承中,trait中的方法相当于复制到当前类中的方法,所以覆盖父类中的同名方法
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:接口, trait , 抽象类, 构成了面向接口开发的三大基石
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!