php 接口,extends,implement,implements 作用及区别收集整理

extends 是继承某个类 ,继承之后可以使用父类的方法 ,也可以重写父类的方法,继承父类,只要那个类不是声明为final或者那个类定义为abstract的就能继承

implements 是实现多个接口,接口的方法一般为空的,必须重写才能使用,可以有效的对,实现的类 方法名,及参数进行约束。可以另类的实现多继承。

class A extends B implements C,D,E{}  class中实现方法体。一个interface可以extends多个其他interface。

interface abc{

  const bbq = 321;

  public function a1(show $acc);

}

class test implements abc{

  public function a1(show $acc){  //必须函数 and 参数

    return 123;

  }

}

$n = new abc();

echo $n->a1();

interface Comparable {
 function compare(self $compare);
}

class String implements Comparable {
 private $string;
 function __construct($string) {
 $this->string = $string;
 }

 function compare(self $compare) {
 if($this->string == $compare->string){
 return $this->string."==".$compare->string."<br>";
 }else{
 return $this->string."!=".$compare->string."<br>";
 }
 }
}

class Integer implements Comparable {
 private $integer;
 function __construct($int) {
 $this->integer = $int;
 }

 function compare(self $compare) {
 if($this->integer == $compare->integer){
 return $this->integer."==".$compare->integer."<br>";
 }else{
 return $this->integer."!=".$compare->integer."<br>";
 }
 }
}

$first_int = new Integer(3);
$second_int = new Integer(4);
$first_string = new String("foo");
$second_string = new String("bar");

echo $first_int->compare($second_int);              // 3!=4
echo $first_int->compare($first_int);               // 3==3
echo $first_string->compare($second_string);        // foo!=bar
echo $first_string->compare($second_int);           // 严重错误

implement 用来拓展方法使用。

//扩展新的Function
var a=function(){};
var b=function(){};
Function.implement({
alert:function(msg){//直接alert输出内容
alert(msg);
},
output:function(msg){//firebug的控制台会输出内容,IE会报错
console.log(msg);
}
});
a.alert('1');
a.output('2');
b.output('3');
原文地址:https://www.cnblogs.com/still-love-you/p/4800522.html