title: How to use PDO connection in other classes?
body: <p>I think I've a problem in understanding how OOP works. I already changed the code that it works, but it isn't the propper way I think. Following scenario (No, I'm not creating a userlogin by myself, its really just for local dev. to understand OOP better):</p>
  
  <p>I've a database.php file:</p>
  
  <pre><code>class Database {
  
      /* Properties */
      private $conn;
      private $dsn = 'mysql:dbname=test;host=127.0.0.1';
      private $user = 'root';
      private $password = '';
  
      /* Creates database connection */
      public function __construct() {
          try {
              $this-&gt;conn = new PDO($this-&gt;dsn, $this-&gt;user, $this-&gt;password);
          } catch (PDOException $e) {
              print "Error!: " . $e-&gt;getMessage() . "";
              die();
          }
          return $this-&gt;conn;
      }
  }
  </code></pre>
  
  <p>So in this class I'm creating a database connection and I return the connection (object?)</p>
  
  <p>Then I have a second class, the famous User class (actually I'm not using autoload, but I know about it):</p>
  
  <pre><code>include "database.php";
  
  class User {
      /* Properties */
      private $conn;
  
      /* Get database access */
      public function __construct() {
          $this-&gt;conn = new Database();
      }
  
      /* Login a user */
      public function login() {
          $stmt = $this-&gt;conn-&gt;prepare("SELECT username, usermail FROM user");
          if($stmt-&gt;execute()) {
              while($rows = $stmt-&gt;fetch()) {
                  $fetch[] = $rows;
              }
              return $fetch;
          }
          else {
              return false;
          }
      }
  }
  </code></pre>
  
  <p>So thatare my two classes. Nothing big, as you see. Now, don't get confued about the function name <code>login</code> - Actually I just try to select some usernames and usermails from database and displaying them. I try to achieve this by:</p>
  
  <pre><code>$user = new User();
  $list = $user-&gt;login();
  
  foreach($list as $test) {
      echo $test["username"];
  }
  </code></pre>
  
  <p>And here comes the problem. When I execute this code, I get the following error message:</p>
  
  <blockquote>
    <p>Uncaught Error: Call to undefined method Database::prepare()</p>
  </blockquote>
  
  <p>And I'm not sure that I really understand what causes this error. </p>
  
  <p><strong>The code works well when I change the following things:</strong></p>
  
  <p>Change <code>$conn</code> in database.php to public instead of private (I think thats bad...? But when its private, I can only execute querys inside of the Database class, I'm right? So should I put all these querys in the Database class? I think that's bad, because in a big project it will get become really big..)</p>
  
  <p>And the second change I've to do is:
  Change <code>$this-&gt;conn-&gt;prepare</code> to <code>$this-&gt;conn-&gt;conn-&gt;prepare</code> in the user.php file. And here I've really no Idea why. </p>
  
  <p>I mean, in the constructor of the <code>user.php</code> I've a <code>$this-&gt;conn = new Database()</code> and since new Database will return me the connection object from DB class, I really don't know why there have to be a second <code>conn-&gt;</code> </p>