What is JSON to PHP Class Generator?
This tool converts JSON data into PHP classes with typed properties, constructors, and fromJson/toJson methods. It supports PHP 8 constructor promotion, PSR-4 namespace declarations, and Laravel Eloquent model generation.
How to Use
- Set the root class name, namespace, and choose between PHP 8 constructor promotion or Laravel Eloquent mode
- Paste your JSON data into the input area
- Click "Generate PHP Code" and copy the output into your project
Why Use This Tool?
Tips & Best Practices
- Use realistic sample data — type inference depends on actual values
- Enable PHP 8 constructor promotion for cleaner, more concise class definitions
- Laravel Eloquent mode generates Model classes with $fillable and $casts for quick integration
- Null values in JSON are mapped to nullable types (?string, ?int, etc.)
- Set the namespace to match your PSR-4 autoloading configuration
Frequently Asked Questions
What is PHP 8 constructor promotion?
PHP 8 constructor promotion lets you declare and initialize class properties directly in the constructor parameters. Instead of writing separate property declarations and constructor assignments, you write: public function __construct(public int $id, public string $name). This produces more concise and readable code.
How are JSON types mapped to PHP?
JSON strings → string, integers → int, decimals → float, booleans → bool, null → mixed (nullable), arrays → array, nested objects → separate PHP classes. Null values produce nullable types like ?string or ?int.
What does Laravel Eloquent mode generate?
Laravel Eloquent mode generates classes that extend Illuminate\Database\Eloquent\Model. It includes $fillable (for mass assignment) and $casts (for automatic type casting of boolean, float, and other non-string/int fields). The fromJson method uses self::create() and a toJsonArray method wraps toArray().
How are nested objects handled?
Nested JSON objects are converted to separate PHP classes. They are ordered children-first, so nested classes appear before the parent class that references them. The parent class uses the nested class type in its constructor and calls the nested class's fromJson/toJson methods.
Is my data sent to a server?
No, all processing happens entirely in your browser. Your JSON data never leaves your device.