Skip to content

Instantly share code, notes, and snippets.

@Shaked
Created August 23, 2015 21:14
Show Gist options
  • Save Shaked/4109ecf73cc3cb95888f to your computer and use it in GitHub Desktop.
Save Shaked/4109ecf73cc3cb95888f to your computer and use it in GitHub Desktop.
Serializing User Object
<?php
class User implements JsonSerializable {
private $name;
private $email;
private $facebookId;
public function __construct($name, $email, $facebookId) {
$this->name = $name;
$this->email = $email;
$this->facebookId = $facebookId;
}
public function jsonSerialize() {
return [
"name" => $this->name,
];
}
}
echo json_encode(new User("shaked", "z@shakedos.com", 12345), JSON_PRETTY_PRINT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment