Last active
October 20, 2015 13:33
-
-
Save FoxxMD/01cc8bfad18cc0e7a8ea to your computer and use it in GitHub Desktop.
Json serialization without dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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