title: json_encode error handling PHP OOP
body: <p>On the server side (PHP) I use json_encode to encode the data sent with ajax request (javascript - extjs).</p>
  
  <p>I want to handle the possible json_encode errors, if possible with PHP OOP.</p>
  
  <p>So far I have the following code, but I'm having some difficulties (I'm inexperienced in php oop):</p>
  
  <p><strong>Class ValidateJson</strong></p>
  
  <pre><code>&lt;?php
  
  class ValidateJson {
  
      private $errors = array();
  
     // public static function validaJson(){
  
      public function validaJson(){
  
          //Get the last JSON error.
          $jsonError = json_last_error();
  
          //If this happen.
          //if(is_null($decoded) &amp;&amp; $jsonError == JSON_ERROR_NONE){
          //   $this-&gt;addJsonErrorMsg('Could not decode JSON!');
          //}
  
  
          switch ($jsonError) {
              case JSON_ERROR_DEPTH:
                      $this-&gt;addJsonErrorMsg('Maximum stack depth exceeded');
                  break;
              case JSON_ERROR_STATE_MISMATCH:
                   $this-&gt;addJsonErrorMsg('Underflow or the modes mismatch');
                  break;
              case JSON_ERROR_CTRL_CHAR:
                   $this-&gt;addJsonErrorMsg('Unexpected control character found');
                  break;
              case JSON_ERROR_SYNTAX:
                   $this-&gt;addJsonErrorMsg('Syntax error, malformed JSON');
                  break;
              case JSON_ERROR_UTF8:
                   $this-&gt;addJsonErrorMsg('Malformed UTF-8 characters, possibly incorrectly encoded');
                  break;
              default:
                   $this-&gt;addJsonErrorMsg(' Unknown error');
                  break;
          }
      }
  
      private function addJsonErrorMsg($error_message){
          $this-&gt;errors[] = $error_message;
      }
  
      public function outJsonError(){
          return $this-&gt;errors;
      }
  
      // ?????
      //     if($jsonError != 0){
      //         throw "JSON Parse Error: " + untyped __call__("json_last_error_msg");
      //
      //    }
  }
  
  ?&gt;
  </code></pre>
  
  <p><strong>Query and output:</strong></p>
  
  <pre><code>&lt;?php
  
  require('conect.php');
  $action = $_REQUEST['action'];
  
  switch($action){
  
    case "create":{
  
      $records = $_POST['records'];
      $data = json_decode(stripslashes($records));
  
      require_once('validate_json.php');
      $valida_json = new ValidateJson();
      $valida_json-&gt;validaJson();
  
     //  $errosJson = $valida_json-&gt;outJsonError();
     //  echo $errors; exit; //return string 'Array'
  
      if(json_last_error() == JSON_ERROR_NONE){
  
          $cars = $data-&gt;{'cars'};
  
          if ($_SERVER["REQUEST_METHOD"] == "POST") {
  
              $sqlQuery = "INSERT INTO the_cars (cars) VALUES (?)";
  
              if($statement = $con-&gt;prepare($sqlQuery)){
                  $statement-&gt;bind_param("s", $cars);
                  $statement-&gt;execute();
                  $success= true;
              }else{
                  $erro = $con-&gt;error;
                  $success = false;
              }
  
              echo json_encode(array(
                  "success" =&gt; $sucess,
                  'errors'=&gt; $erro
              ));
  
              $statement-&gt;close();
              $conexao-&gt;close();
  
              break;
          }
  
     }else{
  
          $errorsJson = $valida_json-&gt;outJsonError();
          $success = false;
  
           echo json_encode(array(
                  "success" =&gt; $sucess,
                  'errorsjson'=&gt; $errorsJson
           ));
      }
  }
  ?&gt;
  </code></pre>