JSON to PHP Class Generator
Paste JSON, get PHP classes — typed properties, constructor promotion, readonly, fromArray/toArray — PHP 7.4 to 8.1+
Options
420 characters
4 classes generated · 3,067 characters
PHP 8.1 readonly properties. PHP 8.0 constructor promotion. PHP 7.4 typed properties. Three major versions, three different syntaxes for the same idea — a class that models a JSON object. Writing these classes by hand is tedious, and forgetting to keep fromArray in sync with the property list is a classic source of bugs.
This generator produces the correct class syntax for your PHP version from any JSON. Paste in, select your target version, copy out. Everything runs in your browser — no data sent anywhere.
PHP’s Evolution for Data Modeling
PHP has steadily added language features that make data-model classes less verbose:
PHP 7.4 — Typed Properties
PHP 7.4 introduced property type declarations, finally bringing type safety to class fields:
class User
{
public string $name;
public int $age;
public function __construct(string $name, int $age) {
$this->name = $name;
$this->age = $age;
}
}
This was a major step: accessing a property before it was initialized now throws a TypeError instead of returning null silently.
PHP 8.0 — Constructor Promotion
PHP 8.0 eliminated the repetitive three-step pattern (declare → list in constructor params → assign in body) with constructor promotion:
class User
{
public function __construct(
public string $name,
public int $age,
) {}
}
The public keyword in the parameter list tells PHP to create a property, accept it as a constructor argument, and assign it — all in one declaration.
PHP 8.1 — Readonly Properties
PHP 8.1 added readonly, which prevents reassignment after construction:
class User
{
public function __construct(
public readonly string $name,
public readonly int $age,
) {}
}
readonly combined with constructor promotion is the closest PHP gets to immutable value objects without a library. It is the recommended style for data transfer objects (DTOs) in modern PHP projects.
How to Use This Tool
- Paste your JSON in the JSON Input panel (or click Load sample to try an example)
- Set the Root class name (e.g.
User,Product,ApiResponse) - Optionally add a Namespace (e.g.
App\Models) - Select your PHP version: 7.4+, 8.0+, or 8.1+
- Toggle
strict_types(recommended: on) andfromArray/toArraymethods - Click Copy or Download .php
The fromArray / toArray Pattern
This generator produces fromArray and toArray static/instance methods as the PHP equivalent of JSON serialization:
public static function fromArray(array $data): self
{
return new self(
name: $data['name'],
age: $data['age'],
);
}
public function toArray(): array
{
return [
'name' => $this->name,
'age' => $this->age,
];
}
To decode a JSON API response:
$data = json_decode($responseBody, true);
$user = User::fromArray($data);
To re-encode for an API call:
$payload = json_encode($user->toArray());
declare(strict_types=1)
Every generated file includes declare(strict_types=1) by default. This makes PHP enforce parameter type checks at call time rather than silently coercing values. For data model classes, it means passing a float where an int is expected will throw a TypeError immediately rather than truncating silently.
Keep this on unless you are integrating with legacy code that passes loosely-typed values.
Namespace Support
The namespace field adds a namespace declaration to the file. For a value of App\Models:
<?php
declare(strict_types=1);
namespace App\Models;
class User { ... }
In Laravel, this maps to app/Models/User.php. In Symfony, the PSR-4 autoloader will map it to the corresponding directory in your src/ folder.
Laravel Integration
Laravel applications use a mix of Eloquent models (which map to database tables) and plain PHP objects for API responses. The generated classes work well as DTOs for typed API responses:
// In a controller or service
$response = Http::get('https://api.example.com/users/1');
$user = User::fromArray($response->json());
// Pass the typed DTO to views or other services
return response()->json($user->toArray());
For casting Eloquent model attributes to complex types, Laravel 9+ also supports custom casts. A DTO with fromArray/toArray maps cleanly to the get/set interface of a custom cast.
Type Mapping
| JSON type | PHP type |
|---|---|
| string | string |
| integer | int |
| float/decimal | float |
| boolean | bool |
| object | nested class |
| array of objects | array (with @var ClassName[] phpdoc) |
| array of primitives | array (with @var string[], @var int[], etc.) |
| null | mixed |
FAQ
Does this tool send my JSON to a server?
No. All processing happens entirely in your browser using JavaScript. Your JSON data never leaves your device.
What PHP version should I use?
Use PHP 8.1+ if your server supports it — readonly properties make the code cleaner and safer. Use 8.0+ if you want constructor promotion without readonly. Use 7.4+ for maximum compatibility.
My JSON has camelCase keys but PHP conventions use camelCase too — is that handled?
Yes. JSON keys are converted to camelCase PHP property names. The original JSON key is preserved in the fromArray string literals ($data['originalKey']) so deserialization works correctly.
Can I add Symfony validation constraints or Laravel validation rules?
The generated classes are plain PHP with no framework dependencies. After generating, add annotations or attributes as needed. For example, add Symfony’s #[Assert\NotBlank] or Laravel’s $rules array manually.
What about nullable fields?
Fields that are null in the input JSON, or missing in some array elements, are typed as mixed. Inspect these manually and narrow the type (e.g. ?string) if you know the actual type.
What if my JSON contains deeply nested objects?
The generator handles arbitrary nesting recursively. Each nested object level produces a new named class. Class names are derived from the parent path using PascalCase — rename them in your editor after generation.