PHP interface-oriented programming coupling design mode simple example _ PHP Tutorial

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

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.