title: Error Handling in a PHP Class
body: <p>Hey there,
  here is a question for you guys.</p>
  
  <p>I have so much times to choose a error handling for classes in PHP.</p>
  
  <p>For Example in Ajax PHP Handling Classes i do it this way:</p>
  
  <pre><code>public function setError($msg) {
      $this-&gt;errors[] = $msg;
  }
  
  public function isFailed() {
      return (count($errors) &gt; 0 ? true : false); // if errors &gt; 0 the request is failed
  }
  
  public function getJsonResp() {
      if($this-&gt;isFailed()) {
          $resp = array('result' =&gt; false, 'message' =&gt; $this-&gt;errors[0]);
      } else {
          $resp = array('result' =&gt; true);
          array_merge($resp, $this-&gt;success_data); // the success data is set later
      }
      return json_encode($resp);
  }
  
  // an example function for a execution of a method would be this
  
  public function switchMethod($method) {
      switch($method) {
          case 'create':
              if(!isset($param1, $param2)) {
                  $this-&gt;setError('param1 or param2 not found');
              } else {
                  $this-&gt;createSomething();
              }
              break;
          default:
              $this-&gt;setError('Method not found');
      }
  }
  </code></pre>
  
  <p>So lets know you what i want to aks for:
  Is there a better solution for error handling?</p>