note 96362 deleted from pdo.construct by danbrown
| From: | danbrown@php.net | Date: | Tue, 23 Feb 2010 15:31:50 +0000 |
| Subject: | note 96362 deleted from pdo.construct by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166284@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
<?php
public function __construct($creds)
{
$type = (isset($creds['type'])) ? $creds['type'] :'mysql';
$username = (isset($creds['user'])) ? $creds['user'] : null ;
$password = (isset($creds['pass'])) ? $creds['pass'] : null ;
$options = (isset($creds['opts'])) ? $creds['opts'] : array() ;
$dsn = $this->createDsn($type, $creds);
parent::__construct($dsn, $username, $password, $options);
}
public function createDsn($type, $creds)
{
$dsn = $type . ':';
$struct = array(
'host' => null, 'port' => null, 'dbname'
=> null,
'unix_socket' => null
);
$dsnarray = array_intersect_key($creds, $struct);
foreach($dsnarray as $key => $value)
{
$dsn .= $key . '=' . $value . ';';
}
$dsn = trim($dsn, ';');
return $dsn;
}
?>
of course you make $struct a muli-dimensional array and base keys on appropriate $type and add a
check for 'unix_socket' and 'host' to make sure they are no both filled in