duanjiyun7391 2017-01-12 02:34
浏览 42
已采纳

经常创建许多PDO对象可能不好?

Few years ago, I read some comment in local PHP community that establishing multiple Database Connection (when using mysqli) might cause the problem. At that time, of course that I didn't understand the actual reason of why, however I sense that its actually might be a problem, not an fatal kind error, but performance kind. (whether is right or wrong..)

Now I'm using PDO and the idea that 'you are not allow to creating multiple db connection in any manner' still suffering me.

I want to know that is really bad idea that referencing same PDO object in multiple object.

class IHavePDO {
    $adaptor;

    public function __construct(PDO $pdo) {
        $this->adaptor = $pdo;
    }

    public function save() {
        // Do something with $this->adaptor
    }
}
  • [tail] I know that it can be avoided using Data Mapper Pattern. (still learning) But can't figure out how can I make the same $objIHavePDO->save() method works, not passing the object as parameter for mapper class like $mapper->save($objIHavePDO).
  • 写回答

1条回答 默认 最新

  • dongzhuang5741 2017-01-12 04:13
    关注

    There is nothing wrong in referencing same PDO object in multiple object. That's actually, is the only way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?