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.
You can use PDO::getAttribute()
with PDO::ATTR_DRIVER_NAME
:
$name = $conn->getAttribute(PDO::ATTR_DRIVER_NAME);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With