title: OOP PHP - Passing methods trough functions
body: <p>I am new to OOP in PHP and I am trying to understand how it work.
  I am trying to implement PDO into a my own class.</p>
  
  <p>This is what I am doing:</p>
  
  <pre><code>class db {
  
      private $options;
  
      function __construct() {
          $this-&gt;options = array(
              'database_host' =&gt; DATABASE_HOST,
              'database_name' =&gt; DATABASE_NAME,
              'database_user' =&gt; DATABASE_USER,
              'database_pass' =&gt; DATABASE_PASS
          );
  
      }
  
      private function connect() {
          try {
              $pdo = new PDO('mysql:host=' . $this-&gt;options['database_host'] . ';dbname=' . $this-&gt;options['database_name'], $this-&gt;options['database_user'], $this-&gt;options['database_pass']);
              $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              $pdo-&gt;exec("set names utf8");
          } catch (PDOException $e) {
              echo 'ERROR: ' . $e-&gt;getMessage();
          }
      }
  
      public function select($table, $what, $where, $custom) {
  
          $query = "SELECT " . $what . " FROM " . $table;
          $child = "1";
          $param = array();
          if ($where)
              foreach ($where as $data =&gt; $value) {
                  if ($child == "1") {
                      $query .= " WHERE " . $data . " = '" . $value . "'";
                      $param[":" . $data] = $value;
                      $child = "next";
                  } else {
                      $query .= " AND " . $data . " = '" . $value . "'";
                      $param[":" . $data] = $value;
                  }
              }
  
          if ($custom)
              $query .= ' ' . $custom;
  
          $statement = $pdo-&gt;prepare($query);
          if ($statement-&gt;execute($param))
              return $statement-&gt;fetchAll(PDO::FETCH_ASSOC);
          return false;
      }
  
  }
  </code></pre>
  
  <p>The problem is that I don't know how to make the function <code>connect()</code> communicating with the below one <code>select()</code>
  When I try to call the <code>select()</code> function, I get this error:</p>
  
  <pre><code>Fatal error: Call to a member function prepare() on a non-object in /Applications/MAMP/htdocs/game/api/class.db.php on line 47
  </code></pre>
  
  <p>I would like that when I call the <code>select()</code> function, the <code>connect()</code> one connects to the DB and makes all its variables available for the other functions of the class...but I am lost.</p>