JSON Unmarshal using PHP 8 Attributes

I was un-marshalling some JSON data onto my class and realised we could use PHP 8 attributes to make it cleaner and more easier to use.

Let me walk you through a quick example, imagine you have some JSON data:

The above data represents a flight departing from London Gatwick to Malta International.

Let’s assume we have the following 2 classes, a Flight class and a FlightRoute class that represents the above data structure.

Flight.php

FlightRoute.php

As you can see we are using the JSON attribute from the library to declare the field in the JSON data and the type if the data is an array of something.

Let’s un-marshal this data :)

and voila!

If you want to use the package you can install it with:

Feel free to contribute https://github.com/mrbenosborne/json-unmarshal

Credit: Hero image, https://php.watch/articles/php-attributes

Experienced Full-stack Developer | PHP | Kubernetes | Go | DevOps | AWS | Google Cloud

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store