Skip to content

Instantly share code, notes, and snippets.

@mikemix
Created September 1, 2015 13:56
Show Gist options
  • Save mikemix/8d66ee1eb8ee0d101116 to your computer and use it in GitHub Desktop.
Save mikemix/8d66ee1eb8ee0d101116 to your computer and use it in GitHub Desktop.
json serializable
<?php
class Person implements \JsonSerializable
{
public function __construct($name, $surname)
{
$this->name = $name;
$this->surname = $surname;
}
public function jsonSerialize()
{
return [
'name' => $this->name,
'surname' => $this->surname,
];
}
}
var_dump(json_encode(new Person('Jan', 'Kowalski')));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment