title: PHP Scope Issue Between Included File Variable, Global Function, And Classes
body: <p>I'm having a problem figuring out why a function within a class isn't properly using the global function for connecting to a database. It can access the function fine and it will set up a connection string, but it connects to the wrong database and I think it has to do with scoping, but not sure how to remedy it.</p>
  
  <p>It's set up something like this</p>
  
  <pre><code>include "variableScript.php"; //contains variable set by another script
  
  //other code
  
  function connectDB(){
     global $scriptVar;
     $db;
  
     if(strcmp($scriptVar, 'DB1') == 0){
        $db = 'DB1';
     }
     else{
        $db = 'DB2';
     }
  
     //DB connection code here, user/pass come elsewhere.
  }
  
  //other code
  
  class A{ //this class is called elsewhere
     // class variables here, one being a database connector
     // class constructor
  
     public function run(){
        $this-&gt;dbConn = connectDB();
        //other code in run function
     }
  
     // other class function
  }
  </code></pre>
  
  <p>Now, I know for sure that $scriptVar is set correctly and that the database selected is correct when it's another script's function (not in a class) using the connectDB function so I think it has to deal with the scoping and the class. As I said, I think it's a scoping issue. When the global function is called from inside the class, I don't think it knows what $scriptVar is. I'm not sure if it's trying to find $scriptVar in the class or elsewhere. When I tried putting global $scriptVar inside class A's function that calls the other, it didn't work out for me.</p>
  
  <p>Anyone know what could be up?</p>