Image for post
Image for post
Convert JSON to a PHP object

Consider that you have example.json with the following content:

{
"firstName": "Temuri",
"lastName": "Takalandze",
"active": true,
"position": {
"title": "Developer",
"department": {
"title": "IT"
}
}
}

and several POPO classes to represent this JSON data:

Department.php

<?phpclass Department
{
/**
* @var string
*/
private $title;
// Getters and Setters here...
}

Position.php

<?phpclass Position
{
/**
* @var string
*/
private $title;
/**
* @var \ABGEO\POPO\Example\Department
*/
private $department;
// Getters and Setters here...
}

Person.php

<?phpclass Person
{
/**
* @var string
*/
private $firstName;
/**
* @var string
*/
private $lastName;
/**…

Temuri Takalandze

Software Engineer, having over 3 years of experience in Back-End development; The author of several PHP libraries and modules; Open Source Contributor.

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