生成对象,而不是在代码中直接new 对象,当类参数或者名称发生变化时,只需要修改工厂就可以,符合开放封闭原则,和单例模式进行结合
请先参考单例模式: http://blog.csdn.net/buyingfei8888/article/details/20949291
<?php
namespace shejimoshi;
include "./php09_single.php";
class factory{
private static $obj_db;
public function createdb(){
if(!self::$obj_db){
self::$obj_db = \Single\Single::getInstance();
}
return self::$obj_db;
}
}
$fac = \shejimoshi\factory::createdb();
var_dump($fac);
?>
运行结果: