title: php variable scope with AJAX calls
body: <p>What scope to php variables have with regards to AJAX calls? So for example, a file <code>main.php</code> creates an instance of a class <code>Test</code>:</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;?php
        require_once 'test.php';
        $test = new Test('some parameter');
      ?&gt;
    &lt;/head&gt;
    &lt;body&gt;&lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>Later on there is a jQuery post event triggered by some event:</p>
  
  <pre><code>$(document).ready(function() {
  
    $.post(   
      "class/start.php",   
      function(data) {
        // some functioning.              
      }, "json");
  
  });
  </code></pre>
  
  <p>Then in <code>start.php</code> I need access to the class I defined in <code>main.php</code>. E.g.:</p>
  
  <pre><code>&lt;?php
    echo json_encode($test-&gt;SomeFunction());
  ?&gt;
  </code></pre>
  
  <p>From testing this I'm fairly sure this isn't possible. I assume the scope of <code>$test</code> dies when <code>main.php</code> comes from the server. So what do I need to do to access the instance of that class? Do I need to add <code>$test</code> as a session variable or is there some other better way?</p>