Created
August 23, 2015 21:18
-
-
Save Shaked/e1fd059c1bf37ee485c5 to your computer and use it in GitHub Desktop.
User Object and JsonUser Object
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 | |
| class User { | |
| private $name; | |
| private $email; | |
| private $facebookId; | |
| public function __construct($name, $email, $facebookId) { | |
| $this->name = $name; | |
| $this->email = $email; | |
| $this->facebookId = $facebookId; | |
| } | |
| public function getName() { | |
| return $this->name; | |
| } | |
| } | |
| class JsonUser implements JsonSerializable { | |
| private $name; | |
| public function __construct($name) { | |
| $this->name = $name; | |
| } | |
| public function jsonSerialize() { | |
| return [ | |
| "name" => $this->name, | |
| ]; | |
| } | |
| } | |
| $user = new User("shaked", "[email protected]", 12345); | |
| doSomethingWith($user); | |
| doSomethingElseWith($user); | |
| echo json_encode(new JsonUser($user->getName()), JSON_PRETTY_PRINT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment