Created
September 1, 2015 13:56
-
-
Save mikemix/8d66ee1eb8ee0d101116 to your computer and use it in GitHub Desktop.
json serializable
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 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