Home > Backend Development > PHP Tutorial > JavaScript design pattern PHP interface-oriented programming Coupling design pattern Simple example

JavaScript design pattern PHP interface-oriented programming Coupling design pattern Simple example

WBOY
Release: 2016-07-29 08:44:39
Original
1460 people have browsed it

Copy code 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 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 "The division operation...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;//Directly here: public $varl = new nothingx(); will make an error.
function __construct(){
$this->varl = new nothingx();
}
function ys($a,$b){
return $this->varl->say();
}
}
/* can also be implemented by inheritance:
class China extends nothingx implements js{
function ys($a,$b){
return parent::say();
}
}
*/
class nothingx{
function say(){
return "I don't do any calculations...just to realize'Coupled design pattern' ...I'm here to make soy sauce...";
}
}
class test{
private $one;
private $two;
public function __construct($x,$y){
$this ->
$this->two=$y;
echo "Object-oriented programming - interface


Class test initialization:
Attribute$ Attribute$two=".$this->two."
";
}
function display(js $a){
return "Computation implemented using PHP interface technology - start the calculation:
".$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 above introduces the simple examples of JavaScript design patterns, PHP interface-oriented programming, coupled design patterns, including the content of JavaScript design patterns. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template