php 接口 implements 使用

主要对类名,类所拥有的方法,以及所传参数起约束和规范做用,感觉跟php abstract 抽象类又有点像。

一,接口的定义和调用

  1. <?php  
  2. interface face1  
  3. {  
  4.  const param = 'test';  
  5.  public function show();  
  6. }  
  7.   
  8. class test implements face1  
  9. {  
  10.  public function show()  
  11.  {  
  12.  echo "interface is run<br>";  
  13.  }  
  14. }  
  15.   
  16. $face = new test();  
  17. echo $face->show();         //inerface is run  
  18. echo face1::param;           //test  
  19. ?>  

说明:上面的例子要注意一点,接口的方法名是show,继承接口的类中必须有show这个方法,要不然就会报错。也就是说接口的方法是假的,真正起作用的是在继承的类中的方法,就是因为这一点,所以我觉得,接口根php的抽象类有点像。

二,对参数约束比较严

  1. <?php  
  2. interface face1  
  3. {  
  4.  public function show(show $show);  
  5. }  
  6.   
  7. // 显示正常  
  8. class test implements face1  
  9. {  
  10.  public function show(show $show)  
  11.  {  
  12.  echo "asdfasdf";  
  13.  }  
  14. }  
  15.   
  16. // 报fatal错误  
  17. class test2 implements face1  
  18. {  
  19.  public function show(aaa $aaa)  
  20.  {  
  21.  }  
  22. }  
  23. ?>  

说明:上面的这个例子报fatal错误的,为什么会报fatal错误呢?原因就在所传参数是aaa $aaa,而不是show $show。继承接口类中,调用接口的方法时,所传参数要和接口中的参数名要一至。不然就会报错。

三,接口间的继承和调用接口传递参数

  1. <?php  
  2. interface face1  
  3. {  
  4.  public function show();  
  5. }  
  6.   
  7. interface face2 extends face1  
  8. {  
  9.  public function show1(test1 $test,$num);  
  10. }  
  11.   
  12. class test implements face2  
  13. {  
  14.  public function show()  
  15.  {  
  16.  echo "ok<br>";  
  17.  }  
  18.   
  19.  public function show1(test1 $test,$num)  
  20.  {  
  21.  var_dump($test);  
  22.  echo $test1->aaaa."$num<br>";  
  23.  }  
  24. }  
  25.   
  26. class test1  
  27. {  
  28.  public $aaaa="this is a test";  
  29.  function fun(){  
  30.  echo ' ===============<br>';  
  31.  }  
  32. }  
  33.   
  34. $show = new test1;  
  35. $show->fun();            //显示===============  
  36. test::show();             //显示ok  
  37. test::show1($show,6);     //object(test1)#1 (1) { ["aaaa"]=>  string(14) "this is a test" } 6  
  38. ?>  

说明:上面的例子可以看到,接口face2继承了接口face1,类test继承了接口face2。不知道你发现没有,class类test当中包括有二个方法,一个是show,一个show1,并且一个也不能少,如果少一个,报fatal错误。show1(test1 $test,$num)中的test1必须根继承类的名子要一样class test1。如果不一样,也会报fatal错误。那如果一个接口被多个类继承,并且类名又不一样,怎么办呢?那就要用self了,下面会提到

四,一个接口多个继承

  1. <?php  
  2. interface Comparable {  
  3.  function compare(self $compare);  
  4. }  
  5.   
  6. class String implements Comparable {  
  7.  private $string;  
  8.  function __construct($string) {  
  9.  $this->string = $string;  
  10.  }  
  11.   
  12.  function compare(self $compare) {  
  13.  if($this->string == $compare->string){  
  14.  return $this->string."==".$compare->string."<br>";  
  15.  }else{  
  16.  return $this->string."!=".$compare->string."<br>";  
  17.  }  
  18.  }  
  19. }  
  20.   
  21. class Integer implements Comparable {  
  22.  private $integer;  
  23.  function __construct($int) {  
  24.  $this->integer = $int;  
  25.  }  
  26.   
  27.  function compare(self $compare) {  
  28.  if($this->integer == $compare->integer){  
  29.  return $this->integer."==".$compare->integer."<br>";  
  30.  }else{  
  31.  return $this->integer."!=".$compare->integer."<br>";  
  32.  }  
  33.  }  
  34. }  
  35.   
  36. $first_int = new Integer(3);  
  37. $second_int = new Integer(4);  
  38. $first_string = new String("foo");  
  39. $second_string = new String("bar");  
  40.   
  41. echo $first_int->compare($second_int);              // 3!=4  
  42. echo $first_int->compare($first_int);               // 3==3  
  43. echo $first_string->compare($second_string);        // foo!=bar  
  44. echo $first_string->compare($second_int);           // 严重错误  
  45. ?>  

说明:从上面的例子中可以看出,一个接口可以被多个类继承,并且类名不一样。同一个类之间可以相互调用,不同类之间不能调用。echo $first_string->compare($second_int);报fatal错误的。

原文地址:https://www.cnblogs.com/jiaosq/p/5829544.html