Skip to content

Instantly share code, notes, and snippets.

@Shaked
Created August 23, 2015 21:18
Show Gist options
  • Select an option

  • Save Shaked/e1fd059c1bf37ee485c5 to your computer and use it in GitHub Desktop.

Select an option

Save Shaked/e1fd059c1bf37ee485c5 to your computer and use it in GitHub Desktop.
User Object and JsonUser Object
<?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