title: PHP: Database Connection Class Constructor Method
body: <p>I'm new to OOP. Originally I was defining variables and assigning values to them within the class and outside of the constructor, but after an OOP lesson in Java today, I was told this is bad style and should be avoided.</p>
  
  <p>Here is my original PHP database connection class that I mocked-up:</p>
  
  <pre><code>class DatabaseConnection {
      private $dbHost = "localhost";
      private $dbUser = "root";
      private $dbPass = "";
      private $dbName = "test";
  
      function __construct() {    
          $connection = mysql_connect($this-&gt;dbHost, $this-&gt;dbUser, $this-&gt;dbPass)
              or die("Could not connect to the database:&lt;br /&gt;" . mysql_error());
          mysql_select_db($this-&gt;dbName, $connection) 
              or die("Database error:&lt;br /&gt;" . mysql_error());
      }
  }
  </code></pre>
  
  <p>Is the above considered okay? Or is the following a better way?</p>
  
  <pre><code>class DatabaseConnection {
      private $dbHost;
      private $dbUser;
      private $dbPass;
      private $dbName;
  
      function __construct() {
          $this-&gt;dbHost = "localhost";
          $this-&gt;dbUser = "root";
          $this-&gt;dbPass = "";
          $this-&gt;dbName = "test";
  
          $connection = mysql_connect($this-&gt;dbHost, $this-&gt;dbUser, $this-&gt;dbPass)
              or die("Could not connect to the database:&lt;br /&gt;" . mysql_error());
          mysql_select_db($this-&gt;dbName, $connection) 
              or die("Database error:&lt;br /&gt;" . mysql_error());
      }
  }
  </code></pre>
  
  <p>What should I be focusing on to make sure I am understanding OOP correctly?</p>