跟逻辑处理服务层一样,数据库操作层主要是为了尽可能的把对数据库的操作代码跟其他层面的代码分离开,让代码有较好的重用性,跟架构美感。/*
作者:moyancheng
创建时间:2012-03-01
最后更新时间:2014-01-11
*/
class dao {
public static $db = 0;
public static $table_prefix = 0;
public function __construct() {
global $CFG;
if(!self::$db) self::$db = new pdoModel($CFG['pdo']);
if(!self::$table_prefix) self::$table_prefix = $CFG['table_prefix'];
unset($CFG);
}
}
数据库操作层的构造函数中,可以根据需要来实例化相关的数据库模块,比如mysql、postgresql或是pdo,之后编写的dao层代码,只要继承dao,就能方便的对数据库进行操作了。
下边,我们根据上一篇文章的例子进行数据库操作层的拓展:class indexController extends controller {
public function __construct() {
parent::__construct();
}
public function indexAction() {
echo 'indexAction is ok
';
$this->service->hello();
}
}
class indexService extends service {
public function __construct() {
parent::__construct();
$this->indexDao = new indexDao();
}
public function hello() {
echo 'indexService is ok
';
$this->indexDao->world();
}
}
class indexDao extends dao{
public function __construct() {
parent::__construct();
}
public function world() {
echo 'indexDao is ok
';
}
}
这回,我们再次访问http://localhost/xxx/?c=index&a=index的时候,输出多了一行indexDao is ok。