Skip to content

Instantly share code, notes, and snippets.

@FoxxMD
Last active October 20, 2015 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FoxxMD/01cc8bfad18cc0e7a8ea to your computer and use it in GitHub Desktop.
Save FoxxMD/01cc8bfad18cc0e7a8ea to your computer and use it in GitHub Desktop.
Json serialization without dependencies
<?php
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
unset($userArray['name']);
$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