Reputation: 28929
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.
Upvotes: 21
Views: 5503
Reputation:
You can use PDO::getAttribute()
with PDO::ATTR_DRIVER_NAME
:
$name = $conn->getAttribute(PDO::ATTR_DRIVER_NAME);
Upvotes: 33
Reputation: 163438
Use getAttribute()
:
http://www.php.net/manual/en/pdo.getattribute.php
$pdo_object->getAttribute(PDO::ATTR_DRIVER_NAME);
Upvotes: 5