Skip to content

Instantly share code, notes, and snippets.

@trianglman
Last active January 3, 2022 16:13
Show Gist options
  • Save trianglman/f46959308f9551feba286056cc03d9b3 to your computer and use it in GitHub Desktop.
Save trianglman/f46959308f9551feba286056cc03d9b3 to your computer and use it in GitHub Desktop.
Userland typed JSON deserializer
class DataContainer {
public function __construct(public string $param1, public int $param2)
{
}
public static function fromJson(string $json): self
{
return call_user_func_array(fn (string $param1, int $param2) => new self($param1, $param2), json_decode($json, true));
}
}
@derickr
Copy link

derickr commented Jan 3, 2022

I think this has a parse error - one too few )s on line 8.

@trianglman
Copy link
Author

Yup. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment