duanchi3109 2010-08-25 13:20
浏览 70
已采纳

PDO课程的最佳实践? [关闭]

I want to create a PDO class for handling data base connections.

Here is what I have:

require('php/packages/store/store_db_settings.php');

class store_pdo
{
    private $DBH; // Data Base Handler

    function __construct() 
    {
        $DBH = new PDO(DB_DSN,DB_USER,DB_PASSWORD);
    }

    public function getHandler()
    {
        return $DBH;
    }

}

I think this seems ok, however I am used to just using things like mysql_query and not really sure what problems I could run into in the future. So I thought the experience here could offer guidance.

Is what I have sufficient? should I make my class Singleton, or use static functions? Is there a best practice?

I don't want to go with this, then I after have several other classes using it, discover that I should have written it differently.

P.S. I just noticed that the best-practices tag is no longer allowed... does that mean questions like this are discouraged now too?

  • 写回答

3条回答 默认 最新

  • duankuang1046 2010-08-25 13:25
    关注

    A Singleton and a static class would both work fine. I can't think of a situation when it would make a difference.

    Make sure that if there ever may be the possibility of using multiple connections, you make your Singleton/Static class multi-connection capable from the start (using an array of connections, or object properties...)

    It could be argued, though, that all these methods create a "God Object" of some sort, are nothing but a glorified global, and go against the principles of real OOP. I asked a question about this once that yielded a lot of great feedback.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应