Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I dont see why that would be making things slow other than the fact that youre constantly switching conncections. The only thing i can suggest here is to allow multiple connections instead of switching them:</p> <pre><code>class Database { protected static $connections; protected $activeConnections = array(); protected static $instance; protected function __construct() { } public static loadConnections(array $connections) { self::$connections = $connections; } public function getConnection($name) { if(!isset($this-&gt;activeConnections[$name]) { if(!isset(self::$connections[$name]) { throw new Exception('Connection "' . $name . '" is not configured.'); } $this-&gt;activeConnections[$name] = new PDO( self::$connections[$name]['dsn'], self::$connections[$name]['username'], self::$connections[$name]['password'] ); } return $this-&gt;activeConnections[$name]; } } // usage Database::loadConnections(array( 'db1' =&gt; array( 'dsn' =&gt; "mysql:host=" . DB_HOST . ";dbname=" . DB_NAME, 'user' =&gt; DB_USER, 'password' =&gt; DB_PASSWORD, ), 'db2' =&gt; array( 'dsn' =&gt; "mysql:host=" . DB_HOST2 . ";dbname=" . DB_NAME2, 'user' =&gt; DB_USER2, 'password' =&gt; DB_PASSWORD2, ))); $conn1 = Database::getInstance()-&gt;getConnection('db1'); $conn2 = Database::getInstance()-&gt;getConnection('db2'); </code></pre> <p>Using something like this you can actually manange several open connections at a time, and they are lazy loaded - meaning you dont actually instantiate a PDO connection until you ask for it with <code>Database::getConnection</code> Likewise you can inject additional DSN's and credentials in at anytime. Personally i would load thes form configuration right on to the class instead of hard coding them with constants int he class. then you could so something like:</p> <pre><code>// gives us an array $config = Config::load('path/to/db/config.yml'); Database::loadConnections($config); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload