title: Uncaught Error: Call to a member function prepare() on null error
body: <p>I know there were a lot of answers related to this error, but I still don't know how to solve it... I'm trying to make the database connection, which would connect to the database and insert user's entered values in it and i got this error. I've created 2 files (with different classes):</p>
  
  <p>Here is a connection file:</p>
  
  <pre><code>&lt;?php
  class Connection {
      // Setting Database Source Name (DSN)
   public function __construct() {
  $dsn = 'mysql:host=localhost;dbname=employees';
  // Setting options
  $options = array (PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION);
  // Making the connection to the database
  try {
  $this-&gt;dbh = new PDO($dsn, 'root', '', $options); 
  }
  catch (PDOException $e) {
  $this-&gt;error = $e-&gt;getMessage();
          }
      }
  }
  $connection = new connection();
  ?&gt;
  </code></pre>
  
  <p>And here is users.php file:</p>
  
  <pre><code>&lt;?php
  include 'connection.php';
  class Users {
  public $name;
  public $surname;
  public $employmentDate;
  public function __construct()
  {
  if(isset($_POST['Submit'])) {
  $this-&gt;name = $_POST['name'];
  $this-&gt;surname = $_POST['surname'];
  $this-&gt;employmentDate = $_POST['employmentDate'];
  }
  }
  // Inserting users values to the database table
  public function insertUserValues() {
   $stmt= 'INSERT INTO employee (name,surname,employment_date) VALUES (:name,:surname,:employmentDate)';
   $stmt = $this-&gt;dbh-&gt;prepare();
   $stmt-&gt;bindValue(':name',$name, PDO::PARAM_STR);
   $stmt-&gt;bindValue(':surname',$surname, PDO::PARAM_STR);
   $stmt-&gt;bindValue(':employmenDate',$employmentDate, PDO::PARAM_STR);
   $stmt-&gt;execute([$this-&gt;name,$this-&gt;surname,$this-&gt;employmentDate]);
  }
  }
  $users = new Users();
  $users-&gt;insertUserValues();
  ?&gt;
  </code></pre>
  
  <p>I guess there are some mistakes in code structure, but I'm just learning, so. The code line which throws the error 18 line in users.php file:</p>
  
  <pre><code>$stmt = $this-&gt;dbh-&gt;prepare();
  </code></pre>
  
  <p>Please someone tell me where I am doing a mistake, thank you for any help.</p>