从PHP的mysql扩展切换到PDO。扩展类以减少代码行 [英] Switching from PHP's mysql extension to PDO. Extend class to reduce lines of code

查看:123
本文介绍了从PHP的mysql扩展切换到PDO。扩展类以减少代码行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我一直以来的意思,以掌握的年龄。将几个小型(单文件)应用程序转换为PDO。我可以使用PDO,连接到数据库和运行查询,所有工作。

So this is something I've been meaning to get to grips with for ages. Converting a few small (single-file) applications to PDO. I can use PDO, connect to a database and run queries, all working.

但是每次我初始化一个新的PDO,我也要运行

setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);

是的,只有一行,但有一天我可能需要添加进一步的东西在那里。对我来说,似乎更好的是以某种方式扩展PDO类,可以包括该行,所以我可以开始我的DB连接与一条线。创建我自己的类,即使在这个阶段只添加一行代码,如果我以后决定添加任何其他东西,让我未来的证明。

But each time I initialise a new PDO, I'm also having to run
setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Yeah it's only one line but one day I might need to add further stuff in there. To me it would seem better to somehow extend the PDO class in a way that can include that line so I can just start my DB connection with a single line. Creating my own class, even if only adding a single line of code at this stage, keeps me future-proofed if I ever decide to add anything else in the future.

我找到了类似的东西...

I've found something like this...

class myPDO extends PDO {
    public function __construct($dsn, $user=null, $pass=null, $options=null) {
        parent::__construct($dsn, $user, $pass, $options);
        $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
}

// THEN A SIMPLE 1 LINER TO CONNECT - USING TRY/CATCH AS WELL OF COURSE
$pdo_conn = new myPDO($cfg['pdo_dsn'], $cfg['pdo_user'], $cfg['pdo_pass'], $cfg['pdo_options']);


$ b b

当扩展类时,我假设我实际上不需要重新创建构造函数。

有一种方法可以整合我的 $ this-> setAttribute (PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION); 在我的类中重新创建构造函数?

When extending the class I assume I don't actually need to re-create the constructor.
Is there a way I can integrate my $this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); into my class without re-creating the constructor function?

我只是开始使用PHP的类语法,所以这将是一个人的一个真正的基本问题,但它会同时回答我一些其他问题。

I'm just getting started with PHP's class syntax so this will be a really basic question for someone but it will answer a few other questions for me at the same time.

推荐答案

这里不需要从PDO扩展。你想要的是创建PDO对象的应用程序中的中心位置。

You do not need to extend from PDO here. What you want is a central place in your application that is creating the PDO object. You can then do the changes you like over time (e.g. integrating some configuration file/system) and extend then centrally.

有一件事是,你创建一个类,你可以随时随地进行你喜欢的更改(例如集成一些配置文件/系统)让我们称之为 PdoFactory

One thing is that you create a class for that, let's call it PdoFactory:

class PdoFactory
{
     /**
      * @return PDO
      */
     public function buildPdo() {
        $pdo = new PDO($dsn = 'xyz', $user = 'abc', $pass = '', $options  = '');
        if ($pdoException = true) {
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        return $pdo;
     }
}

用法:

$factory = new PdoFactory();
$pdo = $factory->buildPdo();

正如你所看到的,这是很容易使用的。除了您调用 buildPdo()函数的地方,您甚至可以放置创建 PdoFactory 的位置。

As you can see, this is very easy to use. You can even put the place where you create the PdoFactory apart from the place where you invoke the buildPdo() function.

这也使得你更清楚地处理两个不同的事情:首先创建Pdo对象(封装到自己的类中)和Pdo对象本身

Also this makes it more clear that you are dealing with two different things here: First creating the Pdo object (encapsulated into a class of it's own) and the Pdo object itself which is just for accessing the database.

我希望这会对您有所帮助。

I hope this helps.

这篇关于从PHP的mysql扩展切换到PDO。扩展类以减少代码行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆