This article mainly introduces the Interface Usage of PHP object-oriented programming. It is necessary for PHP programmers to firmly master the concept. If you need it, you can refer
This article mainly introduces the Interface Usage of PHP object-oriented programming. It is necessary for PHP programmers to firmly master the concept. If you need it, you can refer
Interfaces are an important concept in PHP object-oriented programming. This article describes the usage of PHP interfaces in details in the form of instances. The details are as follows:
Interface: interface
In PHP, we can specify the public external operations that an object should have, and use interfaces to specify.
The common method is the interface. Specifies which public operation methods (interfaces) an object should be used for. This is also called an interface (a set of public operation methods)
Interface (interface structure, public method set)
Public method (interface method)
Definition: A structure used to limit the public operation methods required by an object. It is called an interface)
Syntax: defines the interface structure and uses the interface keyword. Some common methods are defined in the interface.
Interface name {list of public operation methods}
Example:
Interface I _Goods {public function sayName (); public function sayPrice ();}
Note:
1. interface method. The access permission must be public.
2. There can only be public methods in the interface, and member variables cannot exist.
3. The interface can only contain unimplemented methods, also called abstract methods, but does not need abstract keywords.
Class implementation interface, using the keyword implements.
Example:
Interface I _Goods {public function sayName (); public function sayPrice ();} class Goods implements I _Goods {public function sayName () {} public function sayPrice (){}}
In this way, the class that implements this interface must implement all the abstract methods in the interface. It is also certain that this method must be a public external operation method.
Multiple implementations: this function can be theoretically implemented through abstract classes, but the abstract classes are not professional.
The use of interfaces is more professional, because php supports multiple implementations, but only supports single inheritance.
Example:
Interface I _Goods {public function sayName (); public function sayPrice ();} interface I _Shop {public function saySafe ();} class Goods implements I _Goods, I _Shop {public function sayName () {} public function sayPrice () {} public function saySafe (){}}
Interfaces can also be inherited.
Example:
Interface I _Goods {public function sayName (); public function sayPrice ();} interface I _Shop extends I _Goods {public function saySafe ();} class Goods implements I _Shop {public function sayName () {} public function sayPrice () {} public function saySafe (){}}
Php Object Interface Support, class constants can be defined
Example:
Interface I _Goods {const PAI = 3.14; public function sayName (); public function sayPrice ();} interface I _Shop extends I _Goods {public function saySafe ();} class Goods implements I _Shop {public function sayName () {} public function sayPrice () {} public function saySafe () {}} echo Goods: PAI;
Output: 3.14