title: using global DB variable inside classes in PHP
body: <p>How can I use global DB variable inside class?
  Let's say I have this in my config.php</p>
  
  <pre><code>$dbh = new PDO("mysql:host=localhost;dbname=mydb", "root", "");
  </code></pre>
  
  <p>and I want to use this $dbh inside class as follows (MyClass.php)</p>
  
  <pre><code>class MyClass
  {
     public function DoSomething($plogin_id)
     {
          $sql = "SELECT * FROM mytable WHERE login_id = :login_id";      
          $stmt = $dbh-&gt;prepare($sql);    //line 14
          $stmt-&gt;bindParam(':login_id', $plogin_id, PDO::PARAM_STR);
     }
  }
  </code></pre>
  
  <p>And inside my index.php file I am using this MyClass as follows:</p>
  
  <pre><code>include "config.php";
  $MyObject = new MyClass();
  $login_result = $MyObject-&gt;DoSomething("admin");
  </code></pre>
  
  <p>It is giving me error:</p>
  
  <blockquote>
    <p>Fatal error: Call to a member function
    prepare() on a non-object in
    C:\xampp\htdocs\MyProject\admin\includes\classes\MyClass.php
    on line 14</p>
  </blockquote>