title: PHP PDO - Uncaught Error: Call To Undefined Method
body: <p><strong>register.php</strong></p>
  
  <pre><code>    &lt;?php
      require_once 'core/init.php';
  
      if(Input::exists())
      {
          $validate = new Validate();
          $validation = $validate-&gt;check($_POST, array(
              'username' =&gt; array(
                  'required' =&gt; true,
                  'min' =&gt; 2,
                  'max' =&gt; 20,
                  'unique' =&gt; 'users'
              ),
              'password' =&gt; array(
                  'required' =&gt; true,
                  'min' =&gt; 6,
  
              ),
              'password_again' =&gt; array(
                  'required' =&gt; true,
                  'matches' =&gt; 'password'
              ),
              'name' =&gt; array(
                  'required' =&gt; true,
                  'min' =&gt; 2,
                  'max' =&gt; 50
              ),
          ));
  
          if($validation-&gt;passed())
          {
              echo "Passed";
          }
          else
          {
              foreach ($validation-&gt;errors() as $error)
              {
                  echo $error, '&lt;br&gt;';
              }
          }
      }
      ?&gt;
  
      &lt;form action="" method="post"&gt;
          &lt;div class="field"&gt;
              &lt;label for="username"&gt;Username&lt;/label&gt;
              &lt;input type="text" name="username" id="username" value="&lt;?php echo escape(Input::get('username')) ?&gt;" autocomplete="off"&gt;
          &lt;/div&gt;
          &lt;div class="field"&gt;
              &lt;label for="password"&gt;Choose a password&lt;/label&gt;
              &lt;input type="password" name="password" id="password" value="&lt;?php echo escape(Input::get('password')) ?&gt;" autocomplete="off"&gt;
          &lt;/div&gt;
          &lt;div class="field"&gt;
              &lt;label for="password_again"&gt;Enter your password again&lt;/label&gt;
              &lt;input type="password" name="password_again" id="password_again" value="&lt;?php echo escape(Input::get('password_again')) ?&gt;" autocomplete="off"&gt;
          &lt;/div&gt;
          &lt;div class="field"&gt;
              &lt;label for="name"&gt;Your name&lt;/label&gt;
              &lt;input type="text" name="name" id="name" value="&lt;?php echo escape(Input::get('name')) ?&gt;" autocomplete="off"&gt;
          &lt;/div&gt;
          &lt;input type="submit" name="register"&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p><strong>db.php</strong></p>
  
  <pre><code>    &lt;?php
      class db
      {
          private static $_instance = null;
          private $_pdo,
                  $_query,
                  $_error = false,
                  $_results,
                  $_count = 0;
  
          private function __construct()
          {
              try
              {
                  $this-&gt;_pdo = new PDO('mysql:host='.config::get('mysql/host').';dbname='.config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));
              }
              catch(PDOException $e)
              {
                  die($e-&gt;getMessage());
              }
          }
  
          public static function getInstance()
          {
              if(!isset(self::$_instance))
              {
                  self::$_instance = new db();
              }
              return self::$_instance;
          }
  
          public function query($sql, $params = array())
          {
              $this-&gt;_error = false;
              if($this-&gt;_query = $this-&gt;_pdo-&gt;prepare($sql))
              {
                  $x = 1;
                  if(count($params))
                  {
                      foreach($params as $param)
                      {
                          $this-&gt;_query-&gt;bindValue($x, $param);
                          $x++;
                      }
                  }
  
                  if($this-&gt;_query-&gt;execute())
                  {
                      $this-&gt;_results = $this-&gt;_query-&gt;fetchAll(PDO::FETCH_OBJ);
                      $this-&gt;_count = $this-&gt;_query-&gt;rowCount();
                  }
                  else
                  {
                      $this-&gt;_error = true;
                  }
              }
              return $this;
          }
  
          public function action($action, $table, $where = array())
          {
              if(count($where) === 3)
              {
                  $operators = array('=', '&gt;', '&lt;', '&gt;=', '&lt;=');
                  $field = '$where[0]';
                  $operator = '$where[1]';
                  $value = '$where[2]';
  
                  if(in_array($operator, $operators))
                  {
                      $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
  
                      if(!$this-&gt;query($sql, array($value))-&gt;error())
                      {
                          return $this;
                      }
                  }
              }
              return false;
          }
  
          public function get($table, $where)
          {
              return $this-&gt;action('SELECT * ', $table, $where);
          }
  
          public function delete($table, $where)
          {
              return $this-&gt;action('DELETE', $table, $where);
          }
  
          public function insert($table, $fields = array())
          {
              $keys = array_keys($fields);
              $values = '';
              $x = 1;
  
              foreach($fields as $field)
              {
                  $values .= '?';
                  if($x &lt; count($fields))
                  {
                      $values .= ', ';
                  }
                  $x++;
              }
  
              $sql = "INSERT INTO users (`".implode('`, `', $keys)."`) VALUES ({$values})";
  
              if(!$this-&gt;query($sql, $fields)-&gt;error())
              {
                  return true;
              }
              return false;
          }
  
          public function update($table, $id, $fields)
          {
              $set = '';
              $x = 1;
  
              foreach($fields as $name =&gt; $value)
              {
                  $set .= "{$name} = ?";
                  if($x &lt; count($fields))
                  {
                      $set .= ', ';
                  }
                  $x++;
              }
  
              $sql = "UPDATE {$table} SET {$set} WHERE id = {$id}";
  
              if(!$this-&gt;query($sql, $fields)-&gt;error())
              {
                  return true;
              }
              return false;
          }
  
          public function results()
          {
              return $this-&gt;_results;
          }
  
          public function error()
          {
              return $this-&gt;_error;
          }
  
          public function count()
          {
              return $this-&gt;_count;
          }
      }
  </code></pre>
  
  <p><strong>validate.php</strong></p>
  
  <pre><code>    &lt;?php
      class Validate
      {
          private $_passed = false,
                  $_errors = array(),
                  $_db = null;
  
          public function __construct()
          {
              $this-&gt;_db = db::getInstance();
          }
  
          public function check($source, $items = array())
          {
              foreach($items as $item =&gt; $rules)
              {
                  foreach($rules as $rule =&gt; $rule_value)
                  {
                      $value = trim($source[$item]);
                      $item = escape($item);
  
                      if($rule === 'required' &amp;&amp; empty($value))
                      {
                          $this-&gt;addError("{$item} is required");
                      }
                      else if(!empty($value))
                      {
                          switch($rule)
                          {
                              case 'min':
                                  if(strlen($value) &lt; $rule_value)
                                  {
                                      $this-&gt;addError("{$item} must be a minimum of {$rule_value} characters");
                                  }
                              break;
                              case 'max':
                                  if(strlen($value) &gt; $rule_value)
                                  {
                                      $this-&gt;addError("{$item} must be a maximum of {$rule_value} characters");
                                  }
                              break;
                              case 'matches':
                                  if($value != $source[$rule_value])
                                  {
                                      $this-&gt;addError("{$rule_value} must match {$item}");
                                  }
                              break;
                              case 'unique':
                                  $check = $this-&gt;_db-&gt;get($rule_value, array($item, '=', $value));
                                  if($this-&gt;count())
                                  {
                                      $this-&gt;addError("{$item} already exists");
                                  }
                              break;
                          }
                      }
                  }
              }
  
              if(empty($this-&gt;_errors))
              {
                  $this-&gt;passed = true;
              }
              return $this;
          }
  
          private function addError($error)
          {
              $this-&gt;_errors[] = $error;
          }
  
          public function errors()
          {
              return $this-&gt;_errors;
          }
  
          public function passed()
          {
              return $this-&gt;_passed;
          }
      }
  </code></pre>
  
  <p><strong>init.php</strong></p>
  
  <pre><code>    &lt;?php
      session_start();
  
      $GLOBALS['config'] = array
      (
          'mysql' =&gt; array
          (
              'host' =&gt; '127.0.0.1',
              'username' =&gt; 'root',
              'password' =&gt; '',
              'db' =&gt; 'lr'
          ),
          'remember' =&gt; array
          (
              'cookie_name' =&gt; 'hash',
              'cookie_expiry' =&gt; 604800
          ),
          'session' =&gt; array
          (
               'session_name' =&gt; 'user'
          )
      );
  
      spl_autoload_register(function($class) 
      {
          require_once 'classes/'.$class.'.php';
      });
  </code></pre>
  
  <p>register.php contains a registration form. i have not yet wrote the code to store it in the database, i just did the validation of the input. register.php return the error saying undefined method count(). the exact error is </p>
  
  <p>Fatal error: Uncaught Error: Call to undefined method Validate::count() in C:\xampp\htdocs\LoginRegistrationSystem\classes\validate.php:50 Stack trace: #0 C:\xampp\htdocs\LoginRegistrationSystem\register.php(8): Validate->check(Array, Array) #1 {main} thrown in C:\xampp\htdocs\LoginRegistrationSystem\classes\validate.php on line 50</p>
  
  <p>please help, i went through the code 3-4 times, but unable to find the error. thanks!</p>