3v4l.org

run code in 300+ PHP versions simultaneously
<?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);

preferences:
13.4 ms | 405 KiB | 5 Q