title: PDO disconnect and PHP variable reference
body: <p>I'm new to PHP high level. But not new to programming.</p>
  
  <p>We have a software (in dismission but still active) that is killing our DB.</p>
  
  <p>Inside is used PDO the problem after a lot of spent time is PDO disconnection mode and the useof PDO inside a wrap class.</p>
  
  <pre><code>public function connect() {
      if(!$this-&gt;connected){
          $col = 'mysql:host='
                  .$this-&gt;parametri-&gt;getHOST()
                  .';'
                  .'dbname='
                  .$this-&gt;parametri-&gt;getDB()
                  .';'
                  .'charset=utf8';
  
          try {
              // connessione tramite creazione di un oggetto PDO
              $db = new PDO($col , $this-&gt;parametri-&gt;getDBUSER(), 
                                   $this-&gt;parametri-&gt;getPASS());
              $this-&gt;pdoconn=$db;
              $this-&gt;connected=TRUE;
          }
          catch(PDOException $e) {
              $this-&gt;connected=FALSE;
              return NULL;
          }
      }
      return  $this-&gt;pdoconn;
  }
  
  public function getPDO(){
          if ($this-&gt;connected){
              return $this-&gt;pdoconn;
          }else {
              return NULL;
          }
      }
  
  public function disconnect() {
          $this-&gt;pdoconn=null;
          $this-&gt;connected=FALSE;
      }
  </code></pre>
  
  <p>Reading PDO documentation and comments on the official site the connection is freed when  $this->pdoconn=null;
  but it has passed via getPDO().</p>
  
  <p>According to <a href="https://derickrethans.nl/talks/phparch-php-variables-article.pdf" rel="nofollow noreferrer">this article</a> and <a href="http://schlueters.de/blog/archives/125-do-not-use-php-references.html" rel="nofollow noreferrer">this dissertion</a>, somewhere there could be a variable pointing to connection so the connection is never freed; the class think the connection is freed and when request create a new connection losing the last one to the user of the class.</p>
  
  <p>So the idea is to pass back the connection tu null also this or there is another way to protect pdoconn and force to be null.</p>
  
  <pre><code>public function disconnect(&amp;$var) {
       $var=null;
       $this-&gt;pdoconn=null;
       $this-&gt;connected=FALSE;
  }
  </code></pre>
  
  <p>Another way is to build anothe wrap class that never expose the pdo conn and force to execute the query inside it to manage also the disconnection.</p>