php共享db连接(设计模式帮助) [英] php shared db connection ( design pattern help )
问题描述
我使用ezSQL_mysql来做db工作。
我的问题是什么是最好的方法设计应用程序?我应该使用单例模式共享数据库连接吗?我的model类是否扩展了ezSQL_mysql?或者也许我完全脱离这里,需要做别的事情。
我需要的是这样的一个
Controller.php
$ db = new ezSQL_mysql($ db_user,$ db_passwd,$ db_database,$ db_host);
$ user = new User();
$ user-> update_email($ new_email);
$ sale = new Sale();
$ sale->购买($ amount);
User_model.php
class User {
/ *使用$ db connection * /
function update_email(){
/ * do something * /
};
}
Sale_model.php
class Sale {
/ * use $ db connection * /
function purchase(){
/ * do something * /
}
}
..所以小应用程序的需求增长,按照惯例,我决定使用MVC框架,而不是滚动我自己的松散的一组类来管理一个持久化的连接和抽象的数据库层。
所以现在我使用CodeIgniter http:// www。 codeigniter.com/ ,它基本上完成了我想要做的更容易管理的方式。
感谢其他答案。
I have a small php app that I want to build a layer of db abstraction on top of with a few "model" type classes.
I'm using ezSQL_mysql to do the db work.
My question is what is the best way to design the app? Should I use a singleton pattern to share the db connection? Should my "model" classes extend ezSQL_mysql ? Or maybe I'm totally off base here and need to do something else.
What I need is something like this
Controller.php
$db = new ezSQL_mysql($db_user, $db_passwd, $db_database, $db_host);
$user = new User();
$user->update_email($new_email);
$sale = new Sale();
$sale->purchase($amount);
User_model.php
class User {
/* uses $db connection */
function update_email(){
/* do something */
};
}
Sale_model.php
class Sale {
/* uses $db connection*/
function purchase () {
/* do something */
}
}
Okay... so the small app's requirements grew, as per usual and I decided to go with an MVC framework, rather than rolling my own loose set of classes to manage a persistant connection and abstract the database layer.
So now I'm using CodeIgniter http://www.codeigniter.com/ which basically accomplishes what I wanted to do in a easier to manage fashion.
Thanks for the other answers.
这篇关于php共享db连接(设计模式帮助)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!