@ 2016-07-18T17:42:47Z <?php
class MyError implements \JsonSerializable
{
private $name;
private $code;
private $msg;
public function __construct($errorName, $errorCode, $errorMSG)
{
$this->name = $errorName;
$this->code = $errorCode;
$this->msg = $errorMSG;
}
public function jsonSerialize()
{
return get_object_vars($this);
// or
return [
'name' => $this->name,
'code' => $this->code,
'msg' => $this->msg
];
}
}
$error = new MyError("Page not found", 404, "Unfortunately, the page does not exist");
echo json_encode($error);
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.1.0 - 8.1.33 , 8.2.0 - 8.2.29 , 8.3.0 - 8.3.25 , 8.4.1 - 8.4.12 Deprecated: Return type of MyError::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/b9g9h on line 15
{"name":"Page not found","code":404,"msg":"Unfortunately, the page does not exist"} Output for 5.5.0 - 5.5.38 , 5.6.0 - 5.6.40 , 7.0.0 - 7.0.33 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.34 , 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 , 8.0.0 - 8.0.30 {"name":"Page not found","code":404,"msg":"Unfortunately, the page does not exist"} preferences:dark mode live preview ace vim emacs key bindings
128.46 ms | 409 KiB | 5 Q