title: PDO Error - Call to a member function prepare() on a non-object
body: <p>I'm having an issue with a class I created that uploads a file. This class has a parent class which is the database access layer. The uploads looks like this:</p>
  
  <pre><code>&lt;?php
  
  class upload extends bao {
  
      // general variables
      private $basePath = '../../uploads/';
      private $fullMaxWidth = 1000;
      private $mediumMaxWidth = 500;
      private $smallMaxWidth = 250;
      private $errors = 0;
      private $difference = 0;
      private $debugging = false;
  
      private $maxFileSize = 100000000;
  
      // current file variables
      private $currFileName = '';
      private $currFileType = '';
      private $currFileTmpName = '';
      private $currFileError = 0;
      private $currFileSize = 0;
      private $currExt = '';
  
      /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
          This will overwrite any default values with whats supplied
  
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
      public function __construct( $params = null ) {
  
          if($this -&gt; debugging) {
              echo '__construct method';
          }
  
          if( $params ) {
              foreach( $params as $key =&gt; $val ) {
                  $this -&gt; $key = $val;
              }
          }
  
          $this -&gt; initiate();
      }
  
      /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
          Initiates the upload process
  
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
      private function initiate() {
  
          if( $this -&gt; debugging ) {
              echo 'initiate method';
          }
  
          for($i = 0; $i &lt; count($_FILES['files']['name']); $i++) {
              $fileCheck = $this -&gt; checkFileSize( $_FILES['files']['size'][$i] );
              if(!$fileCheck) {
                  $this -&gt; errors++;
              }
  
              $this -&gt; currFileType = $_FILES['files']['type'][$i];
              $this -&gt; currFileTmpName = $_FILES['files']['tmp_name'][$i];
              $fileExtPath = pathinfo($_FILES['files']['name'][$i]);
              $this -&gt; currExt = strtolower($fileExtPath['extension']);
  
              if($this -&gt; errors &lt; 1) {
                  $this -&gt; switcher();
              }
  
          }
  
      }
  
      /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
          Runs a check to see what the file size is
  
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
      private function checkFileSize( $fileSize ) {
  
          if($this -&gt; debugging) {
              echo 'checkFileSize method';
          }
  
          if($fileSize &lt;= $this -&gt; maxFileSize) {
              return true;
          } else {
              return false;
          }
  
      }
  
      /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
          Determines which type of file it is
  
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
      private function switcher() {
  
          if($this -&gt; debugging) {
              echo 'switcher method';
          }
  
          if( $this -&gt; currFileType == 'image/gif' || $this -&gt; currFileType == 'image/png' || $this -&gt; currFileType == 'image/jpeg') {
              $this -&gt; image();
          } else {
              $this -&gt; other();
          }
      }
  
      /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
          Uploads all image files
  
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
      private function image() {
  
          if($this -&gt; debugging) {
              echo 'image method';
          }
  
          $rand = rand(1000, 10000);
          $newFullName = time() .'-'. $_COOKIE['isLogged'] .'-'. $rand .'-full.'. $this -&gt; currExt;
          $newMediumName = time() .'-'. $_COOKIE['isLogged'] .'-'. $rand .'-medium.'. $this -&gt; currExt;
          $newSmallName = time() .'-'. $_COOKIE['isLogged'] .'-'. $rand .'-small.'. $this -&gt; currExt;
          $fullPath = $this -&gt; basePath . $newFileName;   
  
          $info = getimagesize( $this -&gt; currFileTmpName );
  
          $fullWidth = $this -&gt; getNewWidth( $this -&gt; fullMaxWidth, $info[0] );
          $mediumWidth = $this -&gt; getNewWidth( $this -&gt; mediumMaxWidth, $info[0] );
          $smallWidth = $this -&gt; getNewWidth( $this -&gt; smallMaxWidth, $info[0] );
  
          $fullDif = $this -&gt; setDifference( $this -&gt; fullMaxWidth, $info[0] );
          $mediumDif = $this -&gt; setDifference( $this -&gt; mediumMaxWidth, $info[0] );
          $smallDif = $this -&gt; setDifference( $this -&gt; smallMaxWidth, $info[0] );
  
          $fullHeight = $this -&gt; getNewHeight( $info[1], $fullDif );
          $mediumHeight = $this -&gt; getNewHeight( $info[1], $mediumDif );
          $smallHeight = $this -&gt; getNewHeight( $info[1], $smallDif );
  
  
          // set debugging to try to output new image sizes
          if( $this -&gt; debugging ) {
              echo $fullWidth .' - '. $fullHeight .'&lt;br/&gt;';
              echo $mediumWidth .' - '. $mediumHeight .'&lt;br/&gt;';
              echo $smallWidth .' - '. $smallHeight .'&lt;br/&gt;';
          }
  
          $smallImage = imagecreatetruecolor($smallWidth, $smallHeight);
          $mediumImage = imagecreatetruecolor($mediumWidth, $mediumHeight);
          $fullImage = imagecreatetruecolor($fullWidth, $fullHeight);
  
  
          switch($info['mime']) {
              case 'image/gif':
                  $smallImg = imagecreatefromgif( $this -&gt; currFileTmpName );
                  $mediumImg = imagecreatefromgif( $this -&gt; currFileTmpName );
                  $fullImg = imagecreatefromgif( $this -&gt; currFileTmpName );
              break;
  
              case 'image/png':
                  $smallImg = imagecreatefrompng( $this -&gt; currFileTmpName );
                  $mediumImg = imagecreatefrompng( $this -&gt; currFileTmpName );
                  $fullImg = imagecreatefrompng( $this -&gt; currFileTmpName );
              break;
  
              default:
                  $smallImg = imagecreatefromjpeg( $this -&gt; currFileTmpName );
                  $mediumImg = imagecreatefromjpeg( $this -&gt; currFileTmpName );
                  $fullImg = imagecreatefromjpeg( $this -&gt; currFileTmpName );
              break;
          }
  
          imagecopyresampled($smallImage, $smallImg, 0, 0, 0, 0, $smallWidth, $smallHeight, $info[0], $info[1] );
          imagecopyresampled($mediumImage, $mediumImg, 0, 0, 0, 0, $mediumWidth, $mediumHeight, $info[0], $info[1] );
          imagecopyresampled($fullImage, $fullImg, 0, 0, 0, 0, $fullWidth, $fullHeight, $info[0], $info[1] );
  
  
           if($info['mime'] == 'image/gif') {
                imagegif( $smallImage, $this -&gt; basePath . $newSmallName );
                imagegif( $mediumImage, $this -&gt; basePath . $newMediumName );
                imagegif( $fullImage, $this -&gt; basePath . $newFullName );
           } elseif($info['mime'] == 'image/png') {
                imagepng( $smallImage, $this -&gt; basePath . $newSmallName, 8);
                imagepng( $mediumImage, $this -&gt; basePath . $newMediumName, 8);
                imagepng( $fullImage, $this -&gt; basePath . $newFullName, 8);
           } else {
                imagejpeg( $smallImage, $this -&gt; basePath . $newSmallName, 80);
                imagejpeg( $mediumImage, $this -&gt; basePath . $newMediumName, 80);
                imagejpeg( $fullImage, $this -&gt; basePath . $newFullName, 80);
           }
  
          // insert into database
          $stringQuery = "INSERT INTO uploads (uid,smallFilename,mediumFilename,fullFilename,typeFile,status,createdOn,modifiedOn)VALUES(:uid, :smallFilename, :mediumFilename, :fullFilename, :typeFile, :status, :createdOn, :modifiedOn )";
          $binding = array(":uid" =&gt; $_COOKIE['userId'], ":smallFilename" =&gt; $newSmallName, ":mediumFilename" =&gt; $newMediumName, ":fullFilename" =&gt; $newFullName, ":typeFile" =&gt; $this -&gt; currExt, ":status" =&gt; "new", ":createdOn" =&gt; currentTime, ":modifiedOn" =&gt; currentTime );
  
          $insertDB = parent::query( $stringQuery, $binding );
  
           if( !$insertDB ) {
              $this -&gt; errors++;
           }
      }
  
      /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
          Uploads all files minus images
  
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
      private function other() {
  
          if($this -&gt; debugging) {
              echo 'other method';
          }
  
          $rand = rand(1000, 10000);
          $newFullName = time() .'-'. $_COOKIE['isLogged'] .'-'. $rand .'.'. $this -&gt; currExt;
          $fullPath = $this -&gt; basePath . $newFullName;
  
          if(move_uploaded_file($this -&gt; currFileTmpName, $fullPath)){
  
              $stringQuery = "INSERT INTO uploads (uid,fullFilename,title,description,keywords,typeFile,uploadedType,uploadedId,status,createdOn,modifiedOn)VALUES(:uid,:fullFilename,:typeFile,:uploadedType,:uploadedId,:status,:createdOn,:modifiedOn)";
              $binding = array( ":uid" =&gt; $_COOKIE['userId'], ":fullFilename" =&gt; $newFullName, ":typeFile" =&gt; $this -&gt; currExt, ":uploadedType" =&gt; "", ":uploadedId" =&gt; "", ":status" =&gt; "new", ":createdOn" =&gt; currentTime, ":modifiedOn" =&gt; currentTime );
  
              $insertDB = parent::query( $stringQuery, $binding );
  
              if( !$insertDB ) {
                  $this -&gt; errors++;
              }
  
          } else {
              echo 'There was an error';
          }
      }
  
      /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
          Utility methods
  
      %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
  
      // if image does not match size set the difference
      private function setDifference( $fullwidth, $width ) {
  
          if($this -&gt; debugging) {
              echo 'setDifference method';
          }
  
          return $fullwidth / $width;
      }
  
      // calculate the new width
      private function getNewWidth( $fullwidth, $width ) {
  
          if($this -&gt; debugging) {
              echo 'getNewWidth method';
          }
  
          $this -&gt; difference = $fullwidth / $width;
          return round($width * $this -&gt; difference);
      }
  
      // calculate the new height
      private function getNewHeight( $height, $diff ) {
  
          if($this -&gt; debugging) {
              echo 'getNewHeight method';
          }
  
          $newHeight = round($height * $diff );
          return $newHeight;
      }
  
      // creates new image for 3 sizes
      private function createImageTrueFull( $width, $height ) {
  
          if($this -&gt; debugging) {
              echo 'createImageTrueFull method';
          }
  
          return imagecreatetruecolor( $width, $height );
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p>The base database access layer looks like this:</p>
  
  <pre><code>&lt;?php
  
  class bao {
  
      private $environments = array(
  
          'development' =&gt; array(
  
          ),
  
          'test' =&gt; array(
  
          ), 
  
          'review' =&gt; array(
  
          ), 
  
          'production' =&gt; array(
  
          ), 
  
      );
  
      private     $url        = '',
                  $host       = '',
                  $dbname     = '',
                  $user       = '',
                  $pass       = '';
  
      /*
          Variable for connection
      */
      private     $connection;
  
      /*
          Init method
      */
      public function __construct( $params = null ) {
  
          $this -&gt; setConnectionData();
          $this -&gt; overwriteSettings( $params );
          $connection = $this -&gt; connect();
  
      }
  
      public function setConnectionData() {
  
          if( strpos( $_SERVER['HTTP_HOST'], 'dev' ) ) {
              $envVariable = 'development';
          } elseif( strpos( $_SERVER['HTTP_HOST'], 'test' ) ) {
              $envVariable = 'test';
          } elseif( strpos( $_SERVER['HTTP_HOST'], 'review' ) ) {
              $envVariable = 'review';
          } else {
              $envVariable = 'production';
          }
  
          $this -&gt; url = $this -&gt; environments[$envVariable]['url'];
          $this -&gt; host = $this -&gt; environments[$envVariable]['host'];
          $this -&gt; dbname = $this -&gt; environments[$envVariable]['dbname'];
          $this -&gt; user = $this -&gt; environments[$envVariable]['user'];
          $this -&gt; pass = $this -&gt; environments[$envVariable]['pass'];
      }
  
      /*
          Overwrite any required values from default
      */
      private function overwriteSettings( $params ) {
          if( $params ) {
              foreach( $params as $key =&gt; $val ) {
                  $this -&gt; $key = $val;
              }
          }
      }
  
      /*
          Connect to database and then assign to variable
      */
      private function connect() {
          $this -&gt; connection = new PDO( sprintf( 'mysql:host=%s;dbname=%s', $this -&gt; host, $this -&gt; dbname ), $this -&gt; user, $this -&gt; pass );
      }
  
      public function openQuery( $query, $params = null ) {
  
          if( $params ) {
              return $this -&gt; query( $query, $params );
          } else {
              return $this -&gt; query( $query );
          }
      }
  
      public function openFetchall( $sth ) {
          return $this -&gt; fetchAll( $sth );
      }
  
      /*
          Runs actual query
      */
      protected function query( $query, $params = null ) {
  
          $sth = $this -&gt; connection -&gt; prepare( $query );
  
          if( $params ) {
              $sth -&gt; execute( $params );
          } else {
              $sth -&gt; execute();
          }
  
          return $sth;
      }
  
      public function openNumRows( $sth ) {
          return $this -&gt; numRows( $sth );
      }
  
      protected function numRows( $sth ) {
          return $sth -&gt; rowCount();
      }
  
      public function openFetch( $sth ) {
          return $this -&gt; fetch( $sth );
      }
  
      protected function fetch( $sth ) {
          return $sth -&gt; fetch( PDO::FETCH_ASSOC );
      }
  
      protected function fetchAll( $sth ) {
          return $sth -&gt; fetchAll( PDO::FETCH_ASSOC );
      }
  
      protected function lastId( $sth ) {
          return $this -&gt; connection -&gt; lastInsertId();
      }
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>Now when I run the script I get this error:</p>
  
  <pre><code>Fatal error: Call to a member function prepare() on a non-object in /home/content/51/8932751/html/sites/clients/OwnerBusinessLoans/www/dev/lib/helpers/bao.php on line 118
  </code></pre>
  
  <p>Which I don't understand why because I am simply running prepared query from my <code>upload</code> class the same way I have for all my other querys and they ran completely fine and did not show this error. In regards to syntax and the way I am doing things, this is the way I do it for over 10 other classes and I never receive any errors.</p>
  
  <p>In my <code>bao</code> class I did remove the connections details but they are there where I am testing the app.</p>
  
  <p>At first I thought it was query syntax or something so I changed and checked and rechecked all my syntax and that looks fine. If it's that my PDO instance is undefined I don't understand why because its defined the same way and used the same throughout all my other classes.</p>
  
  <p>Any help would be greatly appreciated, I've been racking my brain on this and I feel like it has to be something small!</p>
  
  <p>Thanks</p>