Ejemplo n.º 1
0
 /**
  * Initalizes this class, so a connection can be made to the database. The first argument is a
  * url. If url is not null, the connection parameters will be decoded from the url.
  *
  * The provided url will contain the username, password, hostname, port, and the database name.
  * Take the following url for example.
  *
  * root:secret@mysql.example.com:3603/administration
  *
  * It will connect to mysql.example.com at port 3603. It will use the username root, and use
  * the password secret. The database is named administration.
  * Please note that the scheme and fragment will be ignored.
  *
  * @param string|null $url
  */
 public function __construct($url = null)
 {
     if (null !== $url) {
         if (!filter_var($url, \FILTER_VALIDATE_URL)) {
             $url = 'mysql://' . $url;
         }
         $parsed = parse_url($url);
         if (isset($parsed['host'])) {
             $this->host = $parsed['host'];
         }
         if (isset($parsed['port'])) {
             $this->port = $parsed['port'];
         }
         if (isset($parsed['user'])) {
             $this->username = $parsed['user'];
         }
         if (isset($parsed['pass'])) {
             $this->password = $parsed['pass'];
         }
         if (isset($parsed['path'])) {
             $this->databaseName = trim($parsed['path'], '/');
         }
     }
     parent::__construct();
 }
Ejemplo n.º 2
0
 /**
  * Creates a new PDODatabase with the "Data Source Name", username, password, and extra options.
  *
  * The arguments MUST be compatiable with the PDO constructor.
  *
  * @param string      $dsn
  * @param string|null $username
  * @param string|null $password
  * @param array       $options
  */
 public function __construct($dsn, $username = null, $password = null, array $options = [])
 {
     $this->dsn = $dsn;
     $this->username = $username;
     $this->password = $password;
     $this->options = $options;
     parent::__construct();
 }