Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.