读书人

两个简略的php设计模式

发布时间: 2012-10-23 12:12:22 作者: rapoo

两个简单的php设计模式
1.单例模式
不管多少次实例化类,都只有一个实例存在,适合数据库操作

interface Hello{function say_hello();}class English implements Hello{public function say_hello(){echo "Hello!";}}class Chinese implements Hello{public function say_hello(){echo "你好";}}class speak{public static function factory($type){if($type == 1) $temp = new English();else if($type == 2) $temp = new Chinese();else{die("Not supported!");}return $temp;}}$test = Speak::factory(1);$test->say_hello();

读书人网 >软件开发

热点推荐