Home >Backend Development >PHP Tutorial >Detailed explanation of usage examples of php interface application keywords interface and implements

Detailed explanation of usage examples of php interface application keywords interface and implements

伊谢尔伦
伊谢尔伦Original
2017-07-03 10:14:492269browse

PHP classes are single inheritance, that is, they do not support multiple inheritance. When a class requires the functions of multiple classes, inheritance is powerless. For this reason, PHP introduces class interface technology. The following article mainly introduces to you the relevant information about the keywords interface and implements in PHP. Friends in need can refer to it. Let’s take a look together.

PHP interface

PHP class is single inheritance, that is, it does not support multiple inheritance. When a class requires multiple classes When it comes to functions, inheritance is powerless, so PHP introduces class interface technology.

If all the methods in an abstract class are abstract methods and no variables are declared, and all members in the interface have public permissions, then this special Abstract classes are called interfaces.

The interface is defined using the keyword interface, and the keyword implements is used to implement the methods in the interface, which must be fully implemented.

Application of interfaces in classes

1. Keyword: interface

2. Keyword: implements

1. Introduction and creation of interface

Interface: a special abstract class whose memberspropertiesare all abstract or constant.

Rules:

1. All methods in the class are abstract.

2. There is no need to add abstract for abstract methods.

3. The abstract method attribute of the interface is public.

4. Member attributes must be constants.

The format code is as follows:

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

2. Application and specification of interface

Interface reference is different from class inheritance keywordextends, inheritance can only be single, while interfaces can use the keyword implements to multiple references and separate them with commas

1. Format: ordinary class reference interface

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

2. Format: abstract class application Interface example

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

3. Format: Inherit the parent class and reference the interface to coexist

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

Inherit first and then the interface, single inheritance multiple interfaces.

4. Format: Interface and interface inheritance

interface demo3 extends demo { 
... 
}

The example code is as follows:

<?php 
interface demo { 
 const NAME = "名称"; 
 function fun1(); 
 function fun2(); 
} 
interface demo2 { 
 function fun3(); 
 function fun4(); 
} 
interface demo3 { 
 const TEST = "Test"; 
 function fun5(); 
} 
class MyPc implements demo, demo2 { 
 function fun1() { 
 echo "++++++++++<br />"; 
 } 
 function fun2() { 
 echo "----------<br />"; 
 } 
 function fun3() { 
 echo "1111111111<br />"; 
 } 
 function fun4() { 
 echo "2222222222<br />"; 
 } 
} 
class MyPs extends MyPc implements demo3 { 
 function fun5() { 
 echo "继承类后引用接口"; 
 } 
} 
$p = new MyPs; 
$p->fun1(); 
$p->fun2(); 
$p->fun3(); 
$p->fun4(); 
$p->fun5(); 
?>

For example, the interface is defined using the keyword interface and implemented using the keyword implements Methods in the interface and must be fully implemented.

The example code is as follows:

<?php 
//定义接口 
interface User{ 
 function getDiscount(); 
 function getUserType(); 
} 
//VIP用户 接口实现 
class VipUser implements User{ 
 // VIP 用户折扣系数 
 private $discount = 0.8; 
 function getDiscount() { 
  return $this->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); //可以是更多其他用户类型 
?>

Run the example and the output will be:

VIP user product price: 80 yuan

This example demonstrates a PHP interface Simple application. In this example, the User interface implements the user's discount, and the VipUser class implements the specific discount coefficient. Finally, the product category Goods implements different user quotes based on the User interface.

This example is limited to demonstrating the usage of the PHP interface and does not involve its scientific nature.

Implement multiple interfaces

PHP can also implement multiple interfaces at the same time when inheriting a class:

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

The difference between abstract class and interface

Interface is a special abstract class, which can also be regarded as the specification of a model. The general difference between an interface and an abstract class is as follows:

1. If a subclass implements an interface, it must implement all methods in the interface (whether needed or not); if it inherits an abstract class, it only needs to implement the required Just use the method.

2. If the method name defined in an interface changes, then all subclasses that implement this interface need to update the method name synchronously; and if the method name in an abstract class changes, the method name corresponding to its subclass will It is not affected, it just becomes a new method (relative to the old method implementation).

3. Abstract classes can only be inherited singly. When a subclass needs to implement functions that need to be inherited from multiple parent classes, interfaces must be used.

<?php
//简单定义实现接口
interface UserInterface{ //定义user接口
 function getname();
}
interface TeacherInterface{ //定义teacher接口
 function getLengthofService();
}
class User implements UserInterface{ //实现user接口
 private $name="nostop";
 public function getName(){
  return $this->name;
 }
}
class Teacher implements TeacherInterface{ //实现teacher接口
 private $age=23;
 public function getLengthofService(){
  return $this->age;
 }
}
$user=new User();
echo $user->getName().&#39;<br />&#39;;//nostop
$teacher=new Teacher();
echo $teacher->getLengthofService().&#39;<br />&#39;;//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().&#39;<br />&#39;;
 }
 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 "<br />";
//接口实现用户的折扣
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().&#39;商品价格:&#39;.$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 Detailed explanation of usage examples of php interface application keywords interface and implements. 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