title: Passing PDO object into class - PHP Fatal error: Call to a member function execute() on a non-object
body: <p>I am refactoring a (procedural) PHP Library I wrote a while back into a lightweight OOP framework. I'm getting caught up with trying to pass a PDO object to be used in a class. Here's what I've got so far.</p>
  
  <p><strong>Config.php</strong></p>
  
  <pre><code>&lt;?php
  
  class Config {
      // Database Variables
      private $db_type;
      private $db_host;
      private $db_user;
      private $db_pass;
      private $db_name;
      private $db_path; // for sqlite database path
      private $db_char; // charset
  
      // Site Variables
      private $s_protocol;
      private $s_subdomain;
      private $s_domain;
      private $s_tld;
      private $s_dir;
      private $s_name;
      private $s_description;
      private $s_path;
      private $s_visibility;
      private $s_pipe;
      private $s_apps;
      private $s_hooks;
      private $s_blocks;
      private $s_assets;
  
      // User Default
      private $u_groupid;
  
      public function __construct($config) {
          $this-&gt;set($config);
      }
  
      public function set($config) {
          if (!empty($config) &amp;&amp; is_array($config)) {
              foreach ($config as $k =&gt; $v) {
                  if (property_exists(get_class($this), $k)) {
                      $this-&gt;$k = $v;
                  }
              }
              return true;
          }
          else { return false; }
      }
  
      public function get($config) {
          if (!empty($config)) {
              return $this-&gt;$config;
          }
      }
  
      public function domain() {
          return $this-&gt;get('s_protocol') .'://'. $this-&gt;get('s_domain') . $this-&gt;get('s_tld') .'/'. $this-&gt;get('s_dir');
      }
  }
  ?&gt;
  </code></pre>
  
  <p><strong>Database.php</strong></p>
  
  <pre><code>&lt;?php
  
  class Database extends PDO {
      private $config;
  
      public function __construct($config) {
          $this-&gt;config = $config;
          switch($this-&gt;config-&gt;get('db_type')) {
              case 'mysql':
              case 'pgsql':
                  try {
                      return new PDO(
                                  $this-&gt;config-&gt;get('db_type') .':dbname='. $this-&gt;config-&gt;get('db_name') .';host='. $this-&gt;config-&gt;get('db_host'),
                                  $this-&gt;config-&gt;get('db_user'),
                                  $this-&gt;config-&gt;get('db_pass')
                      );
                  }
                  catch(PDOException $e) {
                      die($e-&gt;getMessage());
                  }
                  break;
              case 'sqlite':
                  try {
                      return new PDO($this-&gt;config-&gt;get('db_type') .':'. $this-&gt;config-&gt;get('db_path'));
                  }
                  catch(PDOException $e) {
                      die($e-&gt;getMessage());
                  }
                  break;
              case 'firebird':
                  try {
                      return new PDO(
                                  $this-&gt;config-&gt;get('db_type') .':dbname='. $this-&gt;config-&gt;get('db_host') .':'. $this-&gt;config-&gt;get('db_path'),
                                  $this-&gt;config-&gt;get('db_user'),
                                  $this-&gt;config-&gt;get('db_pass')
                      );
                  }
                  catch(PDOException $e) {
                      die($e-&gt;getMessage());
                  }
                  break;
              case 'informix':
                  try {
                      return new PDO(
                                  $this-&gt;config-&gt;get('db_type') .':DSN='. $this-&gt;config-&gt;get('db_name'),
                                  $this-&gt;config-&gt;get('db_user'),
                                  $this-&gt;config-&gt;get('db_pass')
                      );
                  }
                  catch(PDOException $e) {
                      die($e-&gt;getMessage());
                  }
                  break;
              case 'oracle':
                  try {
                      return new PDO(
                                  'OCI:dbname='. $this-&gt;config-&gt;get('db_name') .';charset='. $this-&gt;config-&gt;get('db_char'),
                                  $this-&gt;config-&gt;get('db_user'),
                                  $this-&gt;config-&gt;get('db_pass')
                      );
                  }
                  catch(PDOException $e) {
                      die($e-&gt;getMessage());
                  }
                  break;
          }
      }
  
  
  }
  ?&gt;
  </code></pre>
  
  <p><strong>Auth.php</strong></p>
  
  <pre><code>&lt;?php
  
  class Auth {
      // Set Database object
      protected $db;
  
      // User fields in users table
      private $id;
      private $email;
      private $password;
      private $firstname;
      private $lastname;
      private $displayname;
      private $groupid;
      private $ip;
      private $created;
      private $updated;
      private $cookie;
      private $sessionid;
      private $lastlogin;
      private $token;
      private $active;
  
      public function __construct($dbh) {
          $this-&gt;db = $dbh;
      }
  
      public function add($params) {
          $sql = '
              INSERT INTO
                  `users` (
          ';
          $cols = array_keys($params);
          $col_string = implode(', ', $cols);
          $sql .= $col_string .'
                  )
              VALUES (
          ';
          array_walk($cols, function(&amp;$v, $k) { $v = ':'. $v; });
          $col_string = implode(', ', $cols);
  
          $sql .= $col_string .'
                  )
          ';
          $stmt = $this-&gt;db-&gt;prepare($sql);
          $stmt-&gt;execute($params);
  
      }
  
      public function remove($params) {
  
      }
  
      public function update($params) {
  
      }
  
      public function get($params) {
  
      }
  
      protected function set($params) {
          if (!empty($params) &amp;&amp; is_array($params)) {
              foreach ($params as $k =&gt; $v) {
                  if (property_exists(get_class($this), $k)) {
                      $this-&gt;$k = $v;
                  }
              }
              return true;
          }
          else { return false; }
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p><strong>init.php</strong></p>
  
  <pre><code>&lt;?php
  session_start();
  $params = array(
                  'db_type' =&gt; 'mysql',
                  'db_host' =&gt; '127.0.0.1',
                  'db_user' =&gt; 'user',
                  'db_pass' =&gt; 'password',
                  'db_name' =&gt; 'database',
                  'u_groupid' =&gt; 4
              );
  require_once('Config.php');         $c = new Config($params);
  require_once('Database.php');       $db = new Database($c);
  require_once('Auth.php');           $u = new Auth($db);
  
  $user = array(
      'email' =&gt; 'newperson@email.com',
      'password' =&gt; md5('password'),
      'firstname' =&gt; 'Jeff',
      'lastname' =&gt; 'Wilson',
      'displayname' =&gt; 'Jeff Wilson',
      'groupid' =&gt; $c-&gt;get('u_groupid'),
      'ip' =&gt; $_SERVER['REMOTE_ADDR'],
      'created' =&gt; date('Y-m-d H:i:s'),
      'sessionid' =&gt; session_id(),
      'active' =&gt; 1,
  );
  $u-&gt;add($user);
  ?&gt;
  </code></pre>
  
  <blockquote>
    <p>PHP Fatal error:  Call to a member function execute() on a non-object in Auth.php on line 46</p>
  </blockquote>
  
  <p>This is line 46:
  <code>$stmt-&gt;execute($params);</code></p>
  
  <p>As far as I know I'm passing the PDO object to the Auth class correctly. It shouldn't say it's a non-object. Can anyone else see what's wrong here?</p>