PHP Interface Oriented Programming Coupling Design Pattern Simple Example_PHP Tutorial

WBOY
Release: 2016-07-21 15:31:18
Original
851 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...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 China implements js{
public $varl=null;// Directly here: public $varl = new nothingx(); will go wrong.
function __construct(){
$this->varl = new nothingx();
}
function ys($a,$b){
return $this->varl ->say();
}
}
/*It can also be implemented through inheritance:
class China extends nothingx implements js{
function ys($a,$b ){
return parent::say();
}
}
*/
class nothingx{
function say(){
return "I don’t have any operations Doing...just to realize'Coupled Design Pattern'...I am here to make soy sauce..." ;
}
}
class test{
private $one;
private $two;
public function __construct($x,$y){
$this-> ;one=$x;
$this->two=$y;
echo "Object-oriented programming - interface
Class test initialization:
Attribute $one=".$this->one." Attribute $two=".$this->two."
";
}
function display(js $a){
return "Computation implemented using PHP interface technology - start the calculation:< /font>
".$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);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323035.htmlTechArticleCopy the 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 implementen...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!