Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Pragmatic, yet flexible solution.
- interface JsonConvertable {
- function toJson();
- }
- class Mammal implements JsonConvertable {
- private $id;
- function toJson() {
- return json_encode($this->getJsonState());
- }
- protected function getJsonState() {
- return get_object_vars($this);
- }
- }
- class Ape extends Mammal {
- private $name;
- protected function getJsonState() {
- return array_merge(parent::getJsonState(), get_object_vars($this));
- }
- }
- $ape = new Ape;
- var_dump($ape->toJson());
Add Comment
Please, Sign In to add comment