Guest User

Untitled

a guest
Jul 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. // Pragmatic, yet flexible solution.
  2.  
  3. interface JsonConvertable {
  4. function toJson();
  5. }
  6.  
  7. class Mammal implements JsonConvertable {
  8. private $id;
  9.  
  10. function toJson() {
  11. return json_encode($this->getJsonState());
  12. }
  13.  
  14. protected function getJsonState() {
  15. return get_object_vars($this);
  16. }
  17. }
  18.  
  19. class Ape extends Mammal {
  20. private $name;
  21.  
  22. protected function getJsonState() {
  23. return array_merge(parent::getJsonState(), get_object_vars($this));
  24. }
  25. }
  26.  
  27. $ape = new Ape;
  28. var_dump($ape->toJson());
Add Comment
Please, Sign In to add comment