Sometimes it’s necessary to implement JsonSerializable without writing by hand every variable (protected and not) present in class. There is a small shortcut to it:
1 2 3 4 5 6 7 8 |
function jsonSerialize() { $data = array(); foreach (get_class_vars(get_class($this)) as $key=>$val){ $data = $this->{$key}; } return $data; } |