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.

Revisions

  1. Mike Zukowsky created this gist Sep 1, 2015.
    20 changes: 20 additions & 0 deletions json serializable.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    <?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')));