A simple example of PHP interface-oriented programming coupling design mode. Copy the code as follows :? Phpinterfacejs {functionys ($ a, $ B);} classAfimplementsjs {functionys ($ a, $ B) {return addition operation ...... result :. ($ a + $ B) ;}} classJfimplemen
The code is as follows:
Interface js {
Function ys ($ a, $ B );
}
Class Af implements js {
Function ys ($ a, $ B ){
Return "addition operation... the result is:". ($ a + $ B );
}
}
Class Jf implements js {
Function ys ($ a, $ B ){
Return "subtraction... The result is:". ($ a-$ B );
}
}
Class AAf implements js {
Function ys ($ a, $ B ){
Return "multiplication... The result is:". ($ a * $ B );
}
}
Class JJf implements js {
Function ys ($ a, $ B ){
Return "division calculation... The result is:". ($ a/$ B );
}
}
Class Modf implements js {
Function ys ($ a, $ B ){
Return "modulo operation... the result is:". ($ a % $ B );
}
}
Class China implements js {
Public $ varl = null; // public $ varl = new nothingx (); an error occurs.
Function _ construct (){
$ This-> varl = new nothingx ();
}
Function ys ($ a, $ B ){
Return $ this-> varl-> say ();
}
}
/* It can also be implemented using inheritance:
Class China extends nothingx implements js {
Function ys ($ a, $ B ){
Return parent: say ();
}
}
*/
Class nothingx {
Function say (){
Return "I do not perform any operations...'Coupling design mode'... I made soy sauce ......";
}
}
Class test {
Private $ one;
Private $ two;
Public function _ construct ($ x, $ y ){
$ This-> one = $ x;
$ This-> two = $ y;
Echo"Object-oriented programming-interfaceClass test initialization:
Property \ $ one = ". $ this-> one." property \ $ two = ". $ this-> two ."";
}
Function display (js $ ){
Return"Operations implemented using the PHP interface technology -- start operations:". $ A-> ys ($ this-> one, $ this-> two )."";
}
}
$ T = new test (103,2 );
$ T1 = new jf;
$ T2 = new China;
Echo $ t-> display ($ t1 );
Echo $ t-> display ($ t2 );
?>
The http://www.bkjia.com/PHPjc/323035.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323035.htmlTechArticle code is as follows :? Php interface js {function ys ($ a, $ B);} class Af implements js {function ys ($ a, $ B) {return "addition operation ...... result :". ($ a + $ B) ;}} class Jf implemen...