Why use this generator
Modern PHP
Uses typed properties (PHP 7.4+) and a static fromJson factory method. Compatible with PHP 7.4+ and 8.x.
Type mapping
JSON strings become string, integers become int, floats become float, booleans become bool, null becomes null. Arrays stay as array.
fromJson method
Each class includes a static fromJson(string $json): self method that decodes JSON and populates the object.
Conversion explained
1. Parse JSON
Input is parsed into a JavaScript value. The class structure is determined by walking the value tree.
2. Collect classes
For each object, a PHP class is generated with typed public properties and a fromJson factory.
3. Emit PHP
Classes are emitted with proper PHP formatting: <?php opening tag, typed properties, and a static factory method.
JSON to PHP Class Generator — frequently asked questions
Which PHP version is required?
PHP 7.4+ for typed properties. For PHP 7.0-7.3, remove the type annotations. PHP 8.x is fully supported.
Is this compatible with Laravel?
Yes — the generated classes work as plain PHP objects. For Laravel Eloquent models, you'll need to add the Model base class, $fillable property, and table name.
How are nullable fields handled?
Currently, nullable fields use null as the type. Edit the output to use specific nullable types like ?string, ?int.
Why doesn't PHP have generics?
PHP doesn't support generics in the type system. Arrays are typed as array without specifying the value type. Use PHPDoc comments (@var User[]) for IDE autocompletion.
You might also like
JSON to Java
Generate Java POJO classes from JSON
JSON to Python
Generate Python dataclasses from JSON
JSON to Go
Generate Go structs from JSON
JSON to C#
Generate C# classes from JSON
JSON to TypeScript
Generate TypeScript interfaces from JSON
JSON Validator
Validate JSON syntax with detailed error messages