Response.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * To change this license header, choose License Headers in Project Properties.
  4. * To change this template file, choose Tools | Templates
  5. * and open the template in the editor.
  6. */
  7. namespace app\common\api;
  8. /**
  9. * Description of Response
  10. *
  11. * @author sgq
  12. */
  13. class Response{
  14. public $code, $msg, $obj;
  15. const SUCCESS = 200;
  16. const ERROR = 500;
  17. public function __construct($code, $msg, $obj = null) {
  18. $this->setCode($code);
  19. $this->setMsg($msg);
  20. $this->obj = $obj;
  21. }
  22. public function __set($name, $value) {
  23. if (property_exists($this, $name)) {
  24. $this->$name = $value;
  25. } else {
  26. throw new \Exception("Property '$name' does not exist in 'Response' class.");
  27. }
  28. }
  29. public function __get($name) {
  30. if (property_exists($this, $name)) {
  31. return $this->$name;
  32. } else {
  33. throw new \Exception("Property '$name' does not exist in 'Response' class.");
  34. }
  35. }
  36. public function __isset($name) {
  37. return property_exists($this, $name);
  38. }
  39. public function setCode($code) {
  40. if (is_int($code)) {
  41. $this->code = $code;
  42. } else {
  43. throw new \Exception("Invalid code. Please provide an integer.");
  44. }
  45. }
  46. public function setMsg($msg) {
  47. if (is_string($msg)) {
  48. $this->msg = $msg;
  49. } else {
  50. throw new \Exception("Invalid msg. Please provide an string.");
  51. }
  52. }
  53. }