title: Point ob_start to a function inside a class to capture fatal errors
body: <p>I'm trying to create a generic class in PHP that will provide a way to call a web service, parse the returning XML and return a JSON object.<p>
  I ran into fatal errors on servers that do not support CURL and/or JSON and looked for a way to gracefully returning the error in a JSON object back to the client, rather than crashing.<br>After some searching,I found an article that suggested I could call <code>ob_start("fatal_error_handler")</code> and provide a handler function:</p>
  
  <pre><code>function fatal_error_handler($buffer) {
      if (ereg("(error&lt;/b&gt;:)(.+)(&lt;br)", $buffer, $regs) ) {
          $err = preg_replace("/&lt;.*?&gt;/","",$regs[2]);
          $buffer = json_encode(array("errorMessage" =&gt; "Fatal error occurred", "exceptionMessage" =&gt; $err));
      }
      return $buffer;
  } 
  </code></pre>
  
  <p>and calling <code>ob_end_flush</code> at the end of the script. <br>This <strong>worked well</strong>, but I now wanted to add that functionality to my class. I tried, and succeeded, in adding the following constructor and destructor:</p>
  
  <pre><code>    function __construct() {
          ob_start("fatal_error_handler");
      }
  
      function __destruct() {
          ob_end_flush();
      }
  </code></pre>
  
  <p>But when I tried moving the handler function into the class, there was no way I could add it to the <code>ob_start()</code> call. I tried <code>ob_start("$this-&gt;fatal_error_handler")</code>, and <code>ob_start("WebService::fatal_error_handler")</code> (WebService being my class name) - to no avail.<p>My question is, how do I pass a name of a class function to <code>ob_start</code> included in my constructor?<p>A bonus question: am I doing this right, or is there a a better way to handle fatal errors in a way that the client can handle?</p>