title: Stop using `global` in PHP
body: <p>I have a <code>config.php</code> that is included to each page. In config I create an array that looks something like:</p>
  <pre><code>$config = array();
  $config['site_name']      = 'Site Name';
  $config['base_path']      = '/home/docs/public_html/';
  $config['libraries_path'] = $config['base_path'] . '/libraries';
  //etc...
  </code></pre>
  <p>Then I have <code>function.php</code>, that is also included to almost each page, where I have to use <code>global $config</code> to get access to it - and <em>this</em> is what I would like to get rid of!</p>
  <p>How do I access <code>$config</code> in the other parts of my code without using <code>global</code>?</p>
  <p>Could anyone explain, <strong>WHY</strong> I shouldn't use <code>global</code> in my example? Some say it's a bad tone, others say it's not secure?</p>
  <h3>EDIT 1:</h3>
  <p>Example of where and how I use it:</p>
  <pre><code>function conversion($Exec, $Param = array(), $Log = '') {
      global $config;
      $cmd = $config['phppath'] . ' ' . $config['base_path'] . '/' . $Exec;
      foreach ($Param as $s)
      {
          $cmd .= ' ' . $s;
      }
  }
  </code></pre>
  <h3>EDIT 2:</h3>
  <p>Putting all of this in the class, as suggested by <a href="https://stackoverflow.com/users/41360/vilx">Vilx</a>, would be cool but in this case, how would I tie it with the following loop that is extracting config <code>key</code> and <code>value</code> from database. <br>I oversimplified the idea of assigning <code>$config</code> array, here is an example:</p>
  <pre><code>$sql = &quot;SELECT * from settings&quot;;
  $rsc = $db-&gt;Execute($sql);
  if ( $rsc ) {
      while(!$rsc-&gt;EOF) {
          $field = $rsc-&gt;fields['setting_options'];
          $config[$field] = $rsc-&gt;fields['setting_values'];
          @$rsc-&gt;MoveNext();
      }
  }
  </code></pre>
  <h3>EDIT 3:</h3>
  <p>Besides, I have to access other <code>vars</code> from functions that are set in config and it's few of them, e.g.:<code>$db</code>, <code>$language</code> and etc.</p>
  <p>If I put them in the class will it really solve anything? If I use <code>global</code> what does it really change?</p>
  <h3>EDIT 4:</h3>
  <p>I read <a href="https://stackoverflow.com/questions/5166087/php-global-in-functions">PHP global in functions</a> where <a href="https://stackoverflow.com/users/208809/gordon">Gordon</a> explains in the very nice way why you shouldn't use <code>global</code>. I agree on everything but I don't use <code>global</code> in my case to reassign the variables, which will result in, like he said, <code>&lt;-- WTF!!</code>, ;)) yeah agree, it's crazy. But if I just need to access database from a function just by using <code>global $db</code> where is the problem in this case? How do you do this otherwise, without using <code>global</code>?</p>
  <h3>EDIT 5:</h3>
  <p>In the same PHP global in functions <a href="https://stackoverflow.com/users/476/deceze">deceze</a> says: <em>&quot;The one big reason against global is that it means the function is dependent on another scope. This will get messy very quickly.&quot;</em></p>
  <p>But I'm talking here about basic 'INIT'. I basically set <code>define</code> but use <code>vars</code> - well that is wrong in technical way. But your function is not depended on anything - but the name of one var <code>$db</code> that you could keep in mind? It's really global need to use <code>$db</code>, where is the DEPENDENCY here and how to use it otherwise?</p>
  <p><strong>P.S.</strong> I just had a thought, that we're facing the conflict here of two different minds, e.g.: <em>mine</em> (yet NOT well understanding object-oriented programming) and those who could be called gurus (from my current point of view) in OOP - what looks obvious for them for me arises new questions. I think that's why this question is being asked over and over again. Personally for me it's gotten more clear after all but still there are things to clarify.</p>