设计模式
单例模式
1.将构造函数变成私有的
2.提供一个静态方法来返回对象
3.在方法里面加限制条件
class Ren{
public $name;
public static $dx;
private function __construct(){
}
public static function duixiang(){
if(empty(Ren::$dx)){
Ren::$dx = new Ren();
}
return Ren::$dx;
}
}
$r = Ren::duixiang();
$r->name = "张三";
$r1 = Ren::duixiang();
var_dump($r1);
//工厂模式
/*class YunSuan{
public $a;
public $b;
public function jia(){
return $this->a+$this->b;
}
public function jian(){
return $this->a-$this->b;
}
public function cheng(){
return $this->a*$this->b;
}
public function chu(){
return $this->a/$this->b;
}
}*/
//做为父类
/*class YunSuan{
public $a;
public $b;
public function suan(){
}
}
class Jia extends YunSuan{
public function suan(){
return $this->a+$this->b;
}
}
class Jian extends YunSuan{
public function suan(){
return $this->a-$this->b;
}
}
class Yu extends YunSuan{
public function suan(){
return $this->a%$this->b;
}
}
//工厂类
class GongChang{
public static function duixiang($ysf){
switch($ysf){
case "+":
return new Jia();
break;
case "-":
return new Jian();
break;
case "%":
return new Yu();
break;
}
}
}
$y = GongChang::duixiang("-");
$y->a = 10;
$y->b = 5;
echo $y->suan();*/