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.
class Error {
private $name;
private $code;
private $msg;
public function __construct($ErrorName, $ErrorCode, $ErrorMSG){
$this->name = $ErrorName;
$this->code = $ErrorCode;
$this->msg = $ErrorMSG;
}
public function getCode(){
return $this->code;
}
public function getName(){
return $this->name;
}
public function getMsg(){
return $this->msg;
}
public function toJSON(){
$json = "";
return json_encode($json);
}
}
What I want toJSON to return:
{ name: "the content of $name var", code : 1001, msg : error while doing request}