php中m层,mPHP核心框架——数据库操作层的代码实现

跟逻辑处理服务层一样,数据库操作层主要是为了尽可能的把对数据库的操作代码跟其他层面的代码分离开,让代码有较好的重用性,跟架构美感。/*

作者: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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值