Quick start: PHP Adapter mode brief _ PHP Tutorial

Source: Internet
Author: User
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:

 
 
  1. // PHP4
  2. /**
  3. * the HwLib helps programmers everywhere write their first program
  4. * @package HelloWorld
  5. * @version 1
  6. */
  7. class HwLib {
  8. /**
  9. * Say “Hello”
  10. * @deprec this function is going away in the future
  11. * @return string
  12. */
  13. function hello() {
  14. return ‘Hello ‘;
  15. }
  16. /**
  17. * target audience
  18. * @return string
  19. */
  20. function world() {
  21. return ‘World!’;
  22. }
  23. }

The following is an example of database running:

 
 
  1. $hw =& new HwLib;
  2. 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 ):

 
 
  1. // version 2
  2. class HwLib {
  3. function greet() {
  4. return ‘Greetings and Salutations ‘;
  5. }
  6. function world() {
  7. return ‘World!’;
  8. }

Accept and handle (though reluctant) a general question...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.