Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Json serialization without dependencies
abstract class Base {
public function getPublicVars() {
return call_user_func('get_object_vars', $this);
class User extends Base implements \JsonSerializable {
public $name;
public $id;
public $email;
protected $company;
public function jsonSerialize() {
$userArray = $this->getPublicVars();
//transform/add/remove anything you need here
$userArray['username'] = $name;
$userArray['companyId'] = $company->id;
return $userArray;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment