This article describes the simple application of PHP adapter mode. Share it for your reference, as follows:
Adapter Pattern is a bridge between two incompatible interfaces. This type of design pattern is a structural pattern, which combines the functions of two independent interfaces.
This pattern involves a single class that is responsible for joining independent or incompatible interface functions. To give a real example, the card reader is used as an adapter between the memory card and the notebook. You insert the memory card into the card reader and then the card reader into the notebook so that the memory card can be read through the notebook.
Example:
//Suppose that a weather interface is developed using PHPclass Weather{ public static function show(){ $info = array( 'temperature' => '25°C', 'wind' => 'Northwest wind level 3 to 4', 'weather' => 'clear', 'PM2.5' => 60 ); return serialize($info); } } //PHP client call$msg = Weather::show(); $msg_arr = unserialize($msg); echo $msg_arr['weather']; //At this time, if Java and Python also call the weather interface,//But the serialized string is not recognized, but the old interface and old php call cannot be modified.//At this time, you can use a new class inheritance, that is, the adapter mode to modify the returned data format to jsonclass WeatherAdapter extends Weather{ public static function show(){ $info = parent::show(); $info_arr = unserialize($info); return json_encode($info_arr); } } //Java and python can be used with the returned json$msg = WeatherAdapter::show();
Running results:
clear
For more information about PHP related content, please check out the topic of this site:PHP object-oriented programming tutorial》、《Complete collection of PHP array (Array) operation techniques》、《Introduction to PHP basic syntax》、《Summary of PHP operations and operator usage》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.