title: Using Ajax to call PHP object method in the same file
body: <p>I have a PHP file, .../datastuff.php
  On this page a PHP object is instantiated. (The class is written in another file). </p>
  
  <pre><code>public class MyClass{
  
          //....
         public function doAfterClick(){
          //.....
         }
  }
  </code></pre>
  
  <p>So in datastuff.php I do</p>
  
  <pre><code>$MyObject = new MyClass();
   //user specific data is added to $MyObject
  </code></pre>
  
  <p>Now I want to call $MyObject->doAfterClick(), when the user presses a button on datastuff.php</p>
  
  <p>I think AJAX is the general solution for this type of problem. But I am not sure how to do this?
  If I wanted to call a PHP function written on another page I could use AJAX to POST data to that page. But I want to stay on this page, because MyObject needs to call the method.</p>
  
  <p>If I POST back to datastuff.php won't $MyObject be lost? 
  If I make it a Singleton or Global would this then work?
  I looked at this, which is a similar problem, ut is not quite what I am looking for:
  <a href="https://stackoverflow.com/questions/11026907/using-jquery-ajax-to-call-a-php-file-process-but-stay-on-same-page">Using JQuery ajax to call a PHP file, process, but stay on same page</a></p>