Home >Backend Development >PHP Tutorial >Quick Start: A Brief Introduction to PHP Adapter Pattern_PHP Tutorial
There is a lot worth learning about PHP. Here we mainly introduce the PHP adapter mode. Interface changes are a common problem that programmers must (albeit reluctantly) accept and deal with. Program providers modify their code; system libraries are revised; various programming languages and related libraries develop and evolve. One of my children's numerous toys succinctly describes this dilemma: You can't rationalize a person who's out of his place.
Question
How do you avoid the inconvenience caused by API changes of external libraries? If you wrote a library, could you provide a way to allow existing users of your software to upgrade gracefully, even if you have changed your API? How should you change an object's interface to better suit your needs?
Solution
Note: Control Body Pattern
PHP Adapter Pattern is the latest example of Control Body Pattern. The structure of an Adapter is similar to a proxy server (Proxy) and a decorator (Decorator), and the difference between them is that the purpose of the adapter (Adapter) is to change the interface of the encapsulated class, the proxy server (Proxy) and the decorator (Decorator) keeps the interface unchanged.
Sample Code
Let’s see how to protect the application from being affected when the API changes. Suppose you've searched hard for the right library and finally found HwLib, a (hypothetically) set of code designed to send messages. The following is the quoted content:
<ol class="dp-xml"> <li class="alt"><span><span>// PHP4 </span></span></li> <li class=""><span>/** </span></li> <li class="alt"><span>* the HwLib helps programmers everywhere write their first program </span></li> <li class=""><span>* @package HelloWorld </span></li> <li class="alt"><span>* @version 1 </span></li> <li class=""><span>*/ </span></li> <li class="alt"><span>class HwLib { </span></li> <li class=""><span>/** </span></li> <li class="alt"><span>* Say “Hello” </span></li> <li class=""><span>* @deprec this function is going away in the future </span></li> <li class="alt"><span>* @return string </span></li> <li class=""><span>*/ </span></li> <li class="alt"><span>function hello() { </span></li> <li class=""><span>return ‘Hello ‘; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>/** </span></li> <li class="alt"><span>* target audience </span></li> <li class=""><span>* @return string </span></li> <li class="alt"><span>*/ </span></li> <li class=""><span>function world() { </span></li> <li class="alt"><span>return ‘World!’; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>} </span></li> </ol>
The following is an example of library operation:
<ol class="dp-xml"> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">hw</font></span><span> =& new HwLib; </span></span></li> <li class=""> <span>echo $hw-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>hello(), $hw-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>world(); </span> </li> </ol>
HwLib has complete documentation. In the document, the author has clearly stated that the hello() method will not be supported (or even eliminated) in future versions. Next, it is now assumed that the second version of HwLib has been released. A new greet() method replaces hello(). Here is the new version of the library (comments have been extracted):
<ol class="dp-xml"> <li class="alt"><span><span>// version 2 </span></span></li> <li class=""><span>class HwLib { </span></li> <li class="alt"><span>function greet() { </span></li> <li class=""><span>return ‘Greetings and Salutations ‘; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>function world() { </span></li> <li class="alt"><span>return ‘World!’; </span></li> <li class=""><span>} </span></li> </ol>