title: Defining custom superglobal array in PHP
body: <p>How can I define a superglobal array in PHP?</p>
  
  <p>I know that there is no way to do this. However I thought there might be some "brains" out there knowing about a workaround for it. Specifically I want to have an array on every .php file I have that has the same data. I also want to access it like this:</p>
  
  <pre><code>echo $_HANS["foo"];
  </code></pre>
  
  <p>or</p>
  
  <pre><code>if( isset($_HANS["foo"]) &amp;&amp; !empty( $_HANS["foo"] ) ) // DO
  </code></pre>
  
  <p>Important for me is that I don't want to have a static class, where I have to access my members like this:</p>
  
  <pre><code>myClass::myVariable;
  </code></pre>
  
  <p>By now I have something like this, but I guess it's not really efficient:</p>
  
  <pre><code>define( 'HANS', serialize( array( 'FooKey' =&gt; 'FooData' ) ));
  </code></pre>
  
  <p>To access it I have to unserialize it. Storing the data in a variable and THEN I can work with the index-operators.</p>
  
  <pre><code>$_HANS = array();
  foreach(unserialize(HANS) as $Key =&gt; $Data)
  {
      $_HANS[$Key] = $Data;
  }
  echo $_HANS["FooKey"];
  </code></pre>
  
  <p>There must be a way to skip this whole foreach-stuff in every single file.</p>
  
  <p>EDIT =======================================</p>
  
  <p>Okay, so by now I have this little workaround, which should be a little more efficient, than including a whole new file. However I'm still listening for a better solution :)</p>
  
  <p>I simply put this code above in a global function:</p>
  
  <pre><code>function getData()
  {
      $_DATA = array();
      foreach( unserialize(HANS) as $Key =&gt; $Data )
      {
          $_DATA[$Key] = $Data;
      }
      return $_DATA;
  }
  </code></pre>
  
  <p>An everything I have to do is to put this line of code in every .php-file:</p>
  
  <pre><code>$_HANS = getData();
  </code></pre>
  
  <p>The problem about php.ini is the compatibility with companies offering 'webhosting'. They often give you a predefined installation of Apache, PHP, MySQL and Perl (often: <a href="https://en.wikipedia.org/wiki/LAMP_%28software_bundle%29#PHP_and_alternatives" rel="nofollow noreferrer">LAMP</a>) and don't allow you to edit the php.ini file.</p>