In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use the interface in PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Interface
PHP class is single inheritance, that is, it does not support multiple inheritance, when a class needs the functions of multiple classes, inheritance is powerless, so PHP introduces interface technology.
If all methods in an abstract class are abstract methods and no variables are declared, and all members of the interface are public authorized, then this special abstract class is called an interface.
The interface is defined using the interface keyword, and the method of the interface is implemented using implements, and must be fully implemented
Implement an interface
The following is a practical example of the implementation of the PHP interface, where an interface named IPillage is created and implemented. The IPillage interface is as follows:
Interface IPillage {function emptyBakAccount (); function burnDocument ();}
Then implement this interface through the Executive class:
Class Executive extends Employee implements IPillage {private $totalStockOptions; function emptyBankAccount () {echo "Call CFO and ask to transfer funds to Swiss bank account.";} function burnDocuments () {echo "Torch the office suite.";}}
Because people at all levels in the company can encroach on it, you can have an Assistant class to implement this interface:
Class Assistant extends Employee implements IPillage {function takeMome () {echo "Taking memo...";} function emptyBankAccount () {echo "Go on shopping spree with office credit card.";} function burnDocuments () {echo "Start small fire in the trash can.";}}
As you can see, interfaces are particularly useful. Because, although they define how many methods are needed to occur a behavior, and the names of each method, the interface allows different classes to implement these methods in different ways. In this example, for the method of burning files, the Assistdnt class simply burns the files in the trash can, while the Executive class does it in an even worse way (burning its office).
Implement multiple interfaces
It is unfair if we allow outside contractors to encroach on the company, after all, the company is built with the efforts of all full-time employees. That is to say, how to provide employees with the function of working and encroaching on the company, while restricting the contractor to accomplish only the required tasks? The solution is to divide these tasks into several tasks and then implement the necessary multiple interfaces. PHPS supports this feature. Consider the following examples:
The difference between abstract classes and interfaces
Interface is a special abstract class and can also be seen as a model specification. The difference between an interface and an abstract class is as follows:
If a subclass implements an interface, it must implement all the methods in the interface (whether needed or not); if it inherits an abstract class, it only needs to implement the required methods
If the method name defined in an interface changes, all subclasses that implement the interface need to update the method name synchronously, while in an abstract class, if the method name changes, the method name corresponding to its subclass will not be affected, it just becomes a new method.
Abstract classes can only inherit, and interfaces must be used when the functions that a subclass needs to implement need to inherit more than one parent class.
Code example
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.