Home  >  Article  >  Backend Development  >  A detailed introduction to interfaces and implements in PHP

A detailed introduction to interfaces and implements in PHP

零下一度
零下一度Original
2017-06-17 10:50:001303browse

PHP 类是单继承,也就是不支持多继承,当一个类需要多个类的功能时,继承就无能为力了,为此 PHP 引入了类的接口技术。下面这篇文章主要跟大家介绍了关于PHP中关键字interface和implements的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。

PHP 接口

PHP 类是单继承,也就是不支持多继承,当一个类需要多个类的功能时,继承就无能为力了,为此 PHP 引入了类的接口技术。

如果一个抽象类里面的所有方法都是抽象方法,且没有声明变量,而且接口里面所有的成员都是 public 权限的,那么这种特殊的抽象类就叫 接口 。

接口使用关键字 interface 来定义,并使用关键字 implements 来实现接口中的方法,且必须完全实现。

类中接口的应用

      1.关键字:interface

      2.关键字:implements

1.接口的介绍与创建

接口:一种成员属性全部为抽象或常量的特殊抽象类。

规则:

      1.类中全部为抽象方法。

      2.抽象方法钱不用加abstract。

      3.接口抽象方法属性为public。

      4.成员属性必须为常量。

格式代码如下:


interface demo { 
const NAME = "常量对象属性"; 
function fun1(); 
function fun2(); //抽象方法。 
}

2.接口的应用与规范

接口引用区别于类继承关键字 extends ,继承只能只是单一性,而接口可以使用关键字 implements 多个引用并用逗号分开

1.格式:普通类引用接口


class MyPc implements demo , demo2 , demo3 { 
... 
}

2.格式:抽象类应用接口例子


abstract class MyPc implements demo , demo2 , demo3 { 
... 
}

3.格式:继承父类引用接口并存


class MyPc extends Root implements demo , demo2 , demo3 { 
... 
}

先继承后接口,单继承多接口。

4.格式:接口与接口的继承


interface demo3 extends demo { 
... 
}

实例代码如下:


"; 
 } 
 function fun2() { 
 echo "----------
"; } function fun3() { echo "1111111111
"; } function fun4() { echo "2222222222
"; } } class MyPs extends MyPc implements demo3 { function fun5() { echo "继承类后引用接口"; } } $p = new MyPs; $p->fun1(); $p->fun2(); $p->fun3(); $p->fun4(); $p->fun5(); ?>

例,接口使用关键字 interface 来定义,并使用关键字 implements 来实现接口中的方法,且必须完全实现。

实例代码如下:


discount; 
 } 
 function getUserType() { 
  return "VIP用户"; 
 } 
} 
class Goods{ 
 var $price = 100; 
 var $vc; 
 //定义 User 接口类型参数,这时并不知道是什么用户 
 function run(User $vc){ 
  $this->vc = $vc; 
  $discount = $this->vc->getDiscount(); 
 $usertype = $this->vc->getUserType(); 
  echo $usertype."商品价格:".$this->price*$discount; 
 } 
} 
$display = new Goods(); 
$display ->run(new VipUser); //可以是更多其他用户类型 
?>

运行该例子,输出:

VIP用户商品价格:80 元

该例子演示了一个 PHP 接口的简单应用。该例子中,User 接口实现用户的折扣,而在 VipUser 类里面实现了具体的折扣系数。最后商品类 Goods 根据 User 接口来实现不同的用户报价。

该例子仅限于演示 PHP 接口的用法,不涉及其科学与否。

实现多个接口

PHP也可以在继承一个类的时候同时实现多个接口:


class 子类 extends 父类 implemtns 接口1, 接口2, ... 
{ 
 ...... 
}

抽象类和接口的区别

接口是特殊的抽象类,也可以看做是一个模型的规范。接口与抽象类大致区别如下:

      1.一个子类如果 implements 一个接口,就必须实现接口中的所有方法(不管是否需要);如果是继承一个抽象类,只需要实现需要的方法即可。

      2.如果一个接口中定义的方法名改变了,那么所有实现此接口的子类需要同步更新方法名;而抽象类中如果方法名改变了,其子类对应的方法名将不受影响,只是变成了一个新的方法而已(相对老的方法实现)。

      3.抽象类只能单继承,当一个子类需要实现的功能需要继承自多个父类时,就必须使用接口。


name;
 }
}
class Teacher implements TeacherInterface{ //实现teacher接口
 private $age=23;
 public function getLengthofService(){
  return $this->age;
 }
}
$user=new User();
echo $user->getName().'
';//nostop $teacher=new Teacher(); echo $teacher->getLengthofService().'
';//23 //继承类实现接口 class GraduResultudent extends User implements TeacherInterface{ //继承User类 实现接口 private $teacher; public function construct(){ //定义构造函数 $this->teacher=new Teacher(); //实例化Teacher对象 } public function getLengthOfService(){ //实现TeacherInterface接口的方法 return $this->teacher->getLengthOfService(); } } class Result{ public static function getUserName(UserInterface $_user){ //注意:这里面的类型变成接口类型 echo "Name is ".$_user->getName().'
'; } public static function getLengthOfService(TeacherInterface $_teacher){ //注意:这里面的类型变成接口类型 echo "age is ".$_teacher->getLengthOfService(); } } $object=new GraduResultudent(); Result::getUserName($object); //Name is nostop Result::getLengthOfService($object); //age is 23 echo "
"; //接口实现用户的折扣 interface People{ //定义接口 function getUserType(); function getCount(); } class VipUser implements People{ //实现接口 //用户折卡系数 private $discount=0.8; function getUserType(){ return "VIP用户"; } function getCount(){ return $this->discount; } } $vip=new VipUser(); //实现化对象 echo $vip->getUserType().'商品价格:'.$vip->getCount()*100; //VIP用户商品价格:80 class Goods{ var $price=100; var $member; function run(People $member){ //注意:这里面的参数类型是接口类型 $this->member=$member; $discount=$this->member->getCount(); $usertype=$this->member->getUserType(); echo $usertype."商品价格:".$this->price*$discount; } } $display=new Goods(); $display->run(new VipUser);//VIP用户商品价格:80 ?>

The above is the detailed content of A detailed introduction to interfaces and implements in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn