search

Home  >  Q&A  >  body text

PHP class interface technology (interface)

接口技术

What is an interface?

Look at the "development history" of interfaces:

Class--> Abstract class--> A certain "structural form" that is more abstract than abstract class: interface;

Interface: Analogy with abstraction:

There are only abstract methods and constants in the interface;

An abstract class can have abstract methods and other members;

                                                                                                                                                                                                                          Public $p2 = 2;

static $p3=3;

. . . ; . . . }

                                                                                                                                                                                                                                                    interface C1 {

const p1 = 1;

Abstract F3 (); The abstract class of the method is the same form!

However, the reason why it is called an interface is because it stipulates that interfaces can implement multiple inheritance;

##Use of interface:

The purpose of designing an interface is to allow "classes" to inherit it and use its constants and (abstract) methods.

When a class inherits one (or more) interfaces, it is not called inheritance, but called "implements",

That is: a class implements a certain interface(s) ;


## Form:

class class name [extends parent class name] implementations interface name 1, interface name 2,... {

                     … Definition of class members;

                                          

Inheritance of interface:

Interfaces can also inherit from each other - this is called inheritance;

Moreover, they can also perform multiple inheritance!

Indicative code:

Interface I1{.... }

Interface I2{.... }

Interface I3 extends I1, I2{

                                                                              const PI = 3.14; Form: Same as the class, use the interface name and the "::" operator to "get";

echo I3::PI; //Output 3.14

##

A-王青召A-王青召2230 days ago1141

reply all(0)I'll reply

No reply
  • Cancelreply