title: Too many database connection php
body: <p>In my <code>logout.php</code> endpoint I'm using 3 different classes. Each of this 3 classes have:</p>
  
  <pre><code>private $conn;
  private $table_name = "users";
  
  // constructor with $db as database connection
  public function __construct($db)
  {
      $this-&gt;conn = $db;
  }
  </code></pre>
  
  <p>this code in order to connect to database from endpoint while initializing class like so:</p>
  
  <pre><code>// include needed files
  include_once '../config/database.php';
  include_once '../classes/user.php';
  include_once '../classes/token.php';
  
  // instantiate database and product object
  $database = new Database();
  $db = $database-&gt;getConnection();
  
  // initialize object
  $user = new user($db);
  $token = new token($db);
  </code></pre>
  
  <p>In this 3 classes in some functions I sometimes need to use other class so I additionally include <a href="https://gist.github.com/anonymous/4031bbc7ce368d02df999a4faca4a293" rel="nofollow noreferrer">database class</a> once again like in this logout function within <em>user</em> class:</p>
  
  <pre><code>public function logout($receivedToken)
  {
      include_once '../config/database.php';
      include_once '../classes/token.php';
  
      // instantiate database
      $database = new Database();
      $db = $database-&gt;getConnection();
  
      $token = new token($db);
      if($token-&gt;checkToken($receivedToken))
  </code></pre>
  
  <p>And I'm not sure if it is correct approach I'm debugging my code and trying to refactor it to make more sense. Do I need to include this db once again there in logout function within user class in order to use token class? Or maybe I can somehow use this connection which I did initialize in endpoint via <code>__construct</code> and not <code>include</code> database over and over again?</p>