Quick start: PHP Adapter mode overview. PHP has many things worth learning. here we mainly introduce the PHP Adapter mode and interface changes, which are mandatory by programmers (although reluctant) there are many things worth learning about accepting and processing PHP. here we mainly introduce the PHP Adapter mode and interface change, which is a must for programmers (although reluctant) common issues of acceptance and handling. Program providers modify their code, the system Library is corrected, and the development and evolution of various programming languages and related libraries. One of my children's numerous toys briefly describes this dilemma: you cannot reasonably arrange a person you cannot find.
Problem
How do you avoid the inconvenience caused by API changes in external libraries? If you write a library, can you provide a way to allow existing users of your software to perform a perfect upgrade, even if you have changed your API? To better suit your needs, how should you change the interface of an object?
Solution
The PHP Adapter mode provides a completely different interface for objects. You can use an Adapter to implement common interfaces of different classes, while avoiding disputes caused by upgrading and disassembling customer code. Consider when (not a hypothesis !) What will happen to API changes in a third-party library. In the past, you had to clench your teeth and modify all customer code, which is often not that simple. You may be working on a new project that uses the features of the new library, but you already have many old applications, in addition, they interact well with libraries of earlier versions. You will not be able to prove the value of these new features, if this upgrade means that the customer code for other applications will be involved.
Note: Control Body mode
The PHP Adapter mode is the latest example of the control body mode. The structure of an Adapter is similar to that of a Proxy and a Decorator. The difference between the two is that the Adapter aims to change the interface of the encapsulation class, proxy and Decorator maintain the same interface.
Sample code
Let's take a look at how to protect applications from being affected when the API changes. Suppose you have tried your best to find the appropriate library and finally found HwLib, a (hypothetical) code set designed to send information. Reference content is as follows:
- // PHP4
- /**
- * the HwLib helps programmers everywhere write their first program
- * @package HelloWorld
- * @version 1
- */
- class HwLib {
- /**
- * Say “Hello”
- * @deprec this function is going away in the future
- * @return string
- */
- function hello() {
- return ‘Hello ‘;
- }
- /**
- * target audience
- * @return string
- */
- function world() {
- return ‘World!’;
- }
- }
The following is an example of database running:
- $hw =& new HwLib;
- echo $hw->hello(), $hw->world();
HwLib has complete instructions. In this document, the author has clearly pointed out that the hello () method will not be supported (or even eliminated) in future versions ). Next, let's assume that the second version of HwLib has been released. A new greet () method replaces hello (). Below is the new version of the database (the comment has been extracted ):
- // version 2
- class HwLib {
- function greet() {
- return ‘Greetings and Salutations ‘;
- }
- function world() {
- return ‘World!’;
- }
Accept and handle (though reluctant) a general question...