设计模式
1.单例模式
类的计划生育
1.让该类在外界无法造对象
2.让外界可以造一个对象,做一个静态方法返回对象
3.在类里面通过静态变量控
class Dog
{
static $dx;
public $test; private function __construct()
{ } static function DuiXiang()
{
//return new Dog(); if(empty(self::$dx))
{
self::$dx = new Dog();
} return self::$dx;
}
} $a = Dog::DuiXiang(); $b = Dog::DuiXiang();
$b->test="hello"; var_dump($a); //工厂模式
/*class YunSuan
{
public $a;
public $b; function Jia()
{
return $a+$b;
}
function Jian()
{
return $a-$b;
}
}*/ abstract class YuanSuan
{
public $a;
public $b; function Suan()
{
}
} class Jia extends YuanSuan
{
function Suan()
{
return $this->a+$this->b;
}
} class Jian extends YuanSuan
{
function Suan()
{
return $this->a-$this->b;
}
} class Cheng extends YuanSuan
{
function Suan()
{
return $this->a*$this->b;
}
} /*$j = new Cheng();
$j->Suan();
*/ class GongChang
{
static function ShengChan($f)
{
switch($f)
{
case "+":
return new Jia();
break;
case "-":
return new Jian();
break;
case "*":
return new Cheng();
break;
}
}
} $r = GongChang::ShengChan("*");
$r->a=;
$r->b=;
echo $r->Suan();
制返回对象只能是一个