Home > Article > Backend Development > Code analysis for adaptation such as PHP adapter mode
PHPAdapter pattern Code analysis for adaptation
<?php // 适配器模式-类适配 /** * 需要被适配的类 * 需求:给 Source 新增一个新的方法但又不修改 Source 的源代码 */ class Source { public function action() { echo 'call action', '<br/>'; } } interface Targetable { /** * Source 类中同名的方法 */ function action(); /** * 需要给 Source 类新增的方法 */ function action2(); } /** * 适配器类 */ class Adapter extends Source implements Targetable { public function action2() { echo 'call <b>action2</b>', '<br/>'; } } // test code $ad = new Adapter(); $ad->action(); $ad->action2();
The above is the detailed content of Code analysis for adaptation such as PHP adapter mode. For more information, please follow other related articles on the PHP Chinese website!