title: PHP class instance to JSON
body: <p>I'm trying echo the contents of an object in a JSON format. I'm quite unexperienced with PHP and I was wondering if there is a predefined function to do this (like json_encode()) or do you have to build the string yourself?
  When Googling "PHP object to JSON", I'm just finding garbage.</p>
  
  <pre><code>class Error {
      private $name;
      private $code;
      private $msg;
      public function __construct($ErrorName, $ErrorCode, $ErrorMSG){
          $this-&gt;name = $ErrorName;
          $this-&gt;code = $ErrorCode;
          $this-&gt;msg = $ErrorMSG;
      }
      public function getCode(){
          return $this-&gt;code;
      }
      public function getName(){
          return $this-&gt;name;
      }
      public function getMsg(){
          return $this-&gt;msg;
      }
      public function toJSON(){
          $json = "";
  
          return json_encode($json);
      }
  }
  </code></pre>
  
  <p>What I want toJSON to return:</p>
  
  <blockquote>
    <p>{ name: "the content of $name var", code : 1001, msg : error while
    doing request}</p>
  </blockquote>