& Lt ;? Php Adapter mode-object adaptation ** class to be adapted * requirement: add a new method to Source without modifying Source code * classSource {publicfunctionaction () {echocallaction, ;}} interfaceTargetable {*** the same name in the Source class
Object adaptation code analysis in PHP Adapter mode
';}} Interface Targetable {/*** method of the same name in the Source class. * methods not required in the adapter can not be defined in this interface */function action (); /*** method to be added to the Source class */function action2 ();} /*** Adapter class ** is more flexible than class adaptation */class Adapter implements Targetable {/*** does not inherit the Source class, but an instance holding the Source class */private $ sou = null; public function construct (Source $ s) {$ this-> sou = $ s;} public function action () {$ this-> sou-> action ();} public function action2 () {echo 'CallAction2','
';}} // Test code $ s = new Source (); $ ad = new Adapter ($ s); $ ad-> action (); $ ad-> action2 ();
The above is a detailed analysis of object adaptation code in the PHP Adapter mode. For more information, see other related articles in the first PHP community!