Get current PDO driver from existing connection?

pdo, php

Solution

You can use `PDO::getAttribute()` with `PDO::ATTR_DRIVER_NAME`:

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

Problem

I have a class that accepts an existing `PDO` connection in the constructor: ``` class Foo { public function __construct(\PDO $conn = NULL) { // ... } // ... } ``` 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.

Original source