PHP interface-oriented programming coupling design pattern Simple example _php skill

Source: Internet
Author: User
Copy Code code as follows:

<?php
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 operation ... The result is: ". ($a-$b);
}
}
Class AAf implements js{
Function ys ($a, $b) {
Return "multiplication operation ... The result is: ". ($a * $b);
}
}
Class JJf implements js{
Function ys ($a, $b) {
Return "division operation ... The result is: ". ($a/$b);
}
}
Class MODF implements js{
Function ys ($a, $b) {
Return "modulo operation ... The result is: ". ($a% $b);
}
}
class implements js{
Public $varl =null;//here directly: public $varl = new Nothingx (); There will be an error.
function __construct () {
$this->varl = new Nothingx ();
}
Function ys ($a, $b) {
return $this->varl->say ();
}
}
/* can also be implemented in an inherited way yo:
Class extends NOTHINGX implements Js{
Function ys ($a, $b) {
return Parent::say ();
}
}
*/
Class nothingx{
function say () {
Return "I do not do any operations ... Just to realize <font color= #990000 ><b> ' coupling design mode ' </b></font&gt .... I am out of soy sauce ... ";
}
}
Class test{
Private $one;
Private $two;
Public function __construct ($x, $y) {
$this->one= $x;
$this->two= $y;
echo "<font size=20px><strong> Object-oriented programming-interface </font></strong>}
function display (JS $a) {
Return "<font color= #990000 ><b> operations implemented using the PHP interface technology--started:</b></font>}
}
$t =new Test (103,2);
$t 1=new JF;
$t 2=new;
echo $t->display ($t 1);
echo $t->display ($t 2);
?>

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.