Specific introduction to interface and implements in PHP

Source: Internet
Author: User
Tags php class
PHP class is a single inheritance, that is, do not support multiple inheritance, when a class requires multiple classes of functionality, inheritance is powerless, and for this reason PHP introduced the class interface technology. The following article mainly introduce to you about the key words in PHP interface and implements related information, the need for friends can refer to, let's take a look at it.

PHP interface

PHP class is a single inheritance, that is, do not support multiple inheritance, when a class requires multiple classes of functionality, inheritance is powerless, and for this reason PHP introduced the class interface technology.

This special abstract class is called an interface if all the methods inside an abstract class are abstract and do not declare variables, and all members of the interface are public permissions.

The interface is defined using the keyword interface, and the method in the interface is implemented using the keyword implements, and must be fully implemented.

Application of interfaces in class

1. Keywords: interface

2. Keywords: implements

1. Introduction and creation of the interface

Interface: A special abstract class in which member properties are all abstract or constant.

Rules:

1. All of the classes are abstract methods.

2. Abstract method Money is not added to abstract.

3. The interface abstract method property is public.

4. The member property must be a constant.

The format code is as follows:


Interface Demo {Const NAME = "Constant object property"; function fun1 (); function fun2 ();//abstract method. }

2. Application and specification of interface

Interface references differ from class-inheriting keyword extends, where inheritance can only be singular, and interfaces may use keywords to implements multiple references and separate them with commas

1. Format: normal 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 parent class reference interface coexist


Class MyPc extends Root implements demo, Demo2, Demo3 {...}

Inherit the Post interface first, and then inherit the multiple interfaces.

4. Format: interface and interface inheritance


Interface Demo3 extends Demo {...}

The instance code is as follows:


<?php Interface Demo {  const NAME = "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 fu N5 () {  echo "inherits class after reference interface";  }} $p = new Myps; $p->fun1 (); $p->fun2 (); $p->fun3 (); $p->fun4 (); $p->fu N5 ();?>

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

The instance code is as follows:


<?php//define Interface interface user{  function Getdiscount ();  function Getusertype (); }//VIP user interface implementation class Vipuser implements user{  //VIP user discount factor  private $discount = 0.8;  function Getdiscount () {   return $this->discount;  }  function Getusertype () {   return "VIP user";  }} class goods{  var $price = +;  var $vc;  Defines the user interface type parameter, at which point it is not known what the Subscriber  function run (username $vc) {   $this->vc = $VC;   $discount = $this->vc->getdiscount ();  $usertype = $this->vc->getusertype ();   echo $usertype. " Commodity price: ". $this->price* $discount;  } } $display = new Goods (); $display->run (new Vipuser); Can be more other user types?>

To run the example, output:

VIP User Commodity price: 80 yuan

This example demonstrates a simple application of a PHP interface. In this example, the user interface implements a discount for users, and a specific discount factor is implemented within the Vipuser class. Finally, the product class Goods according to the user interface to achieve different customer quotes.

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

Implementing multiple interfaces

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


Class Subclass extends Parent class Implemtns interface 1, interface 2, ... {  ...... }

The difference between an abstract class and an interface

An interface is a special abstract class and can also be seen as a specification of a model. Interfaces are roughly different from abstract classes:

1. A subclass if you implements an interface, you must implement all the methods in the interface (whether or not it is required), or if you are inheriting an abstract class, you only need to implement the required method.

2. If the method name defined in one of the interfaces changes, then all subclasses implementing this interface need to update the method name synchronously, whereas in an abstract class if the method name changes, the corresponding method names of its subclasses are not affected, but only become a new method (relatively old method implementation).

3. Abstract classes can only be inherited, and when a subclass needs to implement functionality that needs to inherit from more than one parent class, the interface must be used.


<?php//simple definition Implementation Interface interface userinterface{//define user interface function GetName ();} Interface teacherinterface{//define Teacher interface function Getlengthofservice ();}  Class User implements userinterface{//implement user interface Private $name = "nostop"; public function GetName () {return $this->name;  }}class Teacher implements teacherinterface{//implement Teacher interface private $age =23; public Function Getlengthofservice () {return $this->age; }} $user =new user (); Echo $user->getname (). ' <br/> ';//nostop$teacher=new teacher (); Echo $teacher->getlengthofservice (). ' <br/> ';//23//inheriting class implementation interface class Graduresultudent extends user implements teacherinterface{//Inherit user class implementation interface private $ Teacher Public Function construct () {//define constructor $this->teacher=new teacher ();//Instantiate Teacher Object} public Function Getlengthofservi CE () {//Implement Teacherinterface interface method return $this->teacher->getlengthofservice ();}} Class result{public static function GetUserName (UserInterface $_user) {//Note: This type becomes the interface type echo "Name is". $_user->getn Ame (). ' &Lt;br/> '; public static function Getlengthofservice (Teacherinterface $_teacher) {//Note: This type becomes the interface type echo "is" .$_teacher-> Getlengthofservice (); }} $object =new graduresultudent (); Result::getusername ($object); Name is Nostopresult::getlengthofservice ($object); Age is 23echo "<br/>";//interface implements user's discount interface people{//define Interface function Getusertype (); function GetCount ();} Class Vipuser implements people{//Implement interface//user folding factor private $discount =0.8; function Getusertype () {return "VIP user";} functio   n GetCount () {return $this->discount; }} $VIP =new Vipuser (); Implement object Echo $vip->getusertype (). ' Commodity price: '. $vip->getcount () *100; VIP User Product Price: 80class goods{var $price =100 var $member; function run (people $member) {//NOTE: The parameter type in this is the interface type $this->membe  R= $member;  $discount = $this->member->getcount ();  $usertype = $this->member->getusertype (); echo $usertype. " Commodity price: ". $this->price* $discount; }} $display =new Goods (), $display->run (new Vipuser),//VIP user commodity price:80?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.