title: How do I create a "mode switch" for PHP PDO?
body: <p>I am currently using PHP PDO to access my database. That is all working absolutely fine and dandy. However, I am going to be adding read-replicas to my server setup, so I wish to adjust my code accordingly.</p>
  
  <p>My current plan of action is to store an array of database credential details. One "read and write" set for the master MySQL database and any number of credentials for "read replicas".</p>
  
  <p>What I wish to do is add a method to the PDO class called "mode" where by a mode is passed through, such as "read" or (default) "write". By passing this through (eg. $dbh->mode("read"); ), it can lookup the details of a random read replica (not fussed which) and use those details for the connection. Then once i'm done reading from my replicas, do another $dbh->mode("default") to put it back into write mode, whereby I can use INSERT, UPDATE etc.</p>
  
  <p>Can this be done without simply destroying the PDO object and creating a new one? Can connection details simply be changed after the object already exists?</p>
  
  <p>So far I have the following (its barely anything, but figured its a start).</p>
  
  <pre><code>Class SwitchablePDO extends PDO
  {
      public function mode($mode = "default") 
      {
          // Use the credentials for my master read and write server by default
  
          if($mode == "read")
          {
              // Use one the credentials for my read replicas (randomly choose)
          }
  
      }
  }
  </code></pre>
  
  <p>Any help regarding this would be appreciated!</p>