从现有连接获取当前的PDO驱动程序? [英] Get current PDO driver from existing connection?

查看:78
本文介绍了从现有连接获取当前的PDO驱动程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在构造函数中接受现有PDO连接的类:

I have a class that accepts an existing PDO connection in the constructor:

class Foo {
    public function __construct(\PDO $conn = NULL) {
        // ...
    }

    // ...
}

我的问题是:有没有一种方法可以确定现有的PDO连接当前正在使用的驱动程序(最好从列表

My question is: is there a way to determine what driver an existing PDO connection is currently using (preferably from the list found here)? I didn't see anything in the API documentation.

出于好奇,我想知道正在使用哪个驱动程序,因为我类中的功能是特定于数据库的,所以我想一种方法来验证传递给它的连接是否具有正确的类型.

For the curious, I'd like to know which driver is being used because functionality in my class is database-specific, so I'd like a way to validate that a connection being passed to it is of the proper type.

推荐答案

您可以使用 PDO::getAttribute() PDO::ATTR_DRIVER_NAME:

$name = $conn->getAttribute(PDO::ATTR_DRIVER_NAME);

这篇关于从现有连接获取当前的PDO驱动程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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