Tool 39 / 50

JSON to PHP 8.1+ DTO Converter

Instantly infer type-safe PHP 8.1+ readonly class models, Constructor Property Promotion, and Spatie Laravel-Data DTOs.

SAMPLE JSON PAYLOAD
PHP SOURCE CODE (.PHP)

What is a JSON to PHP 8.1+ DTO Converter?

A JSON to PHP DTO Converter is a modern backend software engineering utility that transforms JSON payloads into strictly typed PHP 8.1 / PHP 8.2+ Data Transfer Objects (DTOs). Modern PHP development in frameworks like Laravel, Symfony, and Spiral has transitioned from loose associative arrays ($data['user_id']) into strongly typed, immutable class architectures utilizing Constructor Property Promotion and readonly class declarations.

Manually writing PHP classes with docblocks, parameter type hints, and instantiation factories is tedious. Our generator parses the Abstract Syntax Tree (AST) of your sample JSON payload, identifies PHP scalar primitives (string, int, float, bool, array), decomposes nested objects into modular sub-classes, generates clean fromArray() hydration factories, and produces compatible models for Spatie Laravel-Data.

Why Modern PHP & Laravel Developers Need DTO Generation

Creating strongly typed PHP DTOs solves critical backend challenges:

Step-by-Step Code Generation Example

The following real-world example illustrates how a user account payload is compiled into an immutable PHP 8.2 readonly class with constructor promotion and a fromArray() hydration factory.

Input: JSON API Payload

{
  "user_id": 10842,
  "full_name": "Taylor Otwell",
  "email_address": "[email protected]",
  "account_balance_usd": 8450.50,
  "is_active_subscriber": true,
  "company_profile": {
    "company_name": "Laravel LLC",
    "website": "https://laravel.com"
  }
}

Output: Clean PHP 8.1+ Readonly Class

<?php

declare(strict_types=1);

namespace App\DTOs;

readonly class UserDTO
{
    public function __construct(
        public ?int $userId = null,
        public ?string $fullName = null,
        public ?string $emailAddress = null,
        public ?float $accountBalanceUsd = null,
        public ?bool $isActiveSubscriber = null,
        public ?UserDTO_CompanyProfile $companyProfile = null
    ) {}

    public static function fromArray(array $data): self
    {
        return new self(
            userId: $data['user_id'] ?? null,
            fullName: $data['full_name'] ?? null,
            emailAddress: $data['email_address'] ?? null,
            accountBalanceUsd: $data['account_balance_usd'] ?? null,
            isActiveSubscriber: $data['is_active_subscriber'] ?? null,
            companyProfile: isset($data['company_profile']) ? UserDTO_CompanyProfile::fromArray($data['company_profile']) : null
        );
    }
}

Constructor Property Promotion & Readonly Classes

Our generator harnesses modern PHP 8.1+ syntax innovations:

PHP Type Mapping & Nullability Mechanics

Our engine ensures strict type fidelity:

  1. Nullable Union Types (`?type`): Prepends a question mark (?int, ?string) to allow null values when JSON keys are missing.
  2. Floating-Point vs Integer Separation: Distinguishes whole numbers (int) from fractional decimals (float).
  3. Hydration Factory (`fromArray`): Generates static factory methods that map snake_case array keys to camelCase constructor arguments using PHP 8 named arguments.

Symfony Serializer Component Integration

In Symfony enterprise applications, pairing these DTOs with the Symfony\Component\Serializer component allows direct object denormalization without custom array transformations:

$dto = $serializer->deserialize($jsonContent, UserDTO::class, 'json');

Implementing PHP's `JsonSerializable` Interface

To make your DTOs directly serializable back to JSON via json_encode($dto), implement JsonSerializable:

public function jsonSerialize(): mixed
{
    return get_object_vars($this);
}

Nested Collections with PHPDoc Generic Annotations

Because PHP native types cannot express generic array parameters (e.g. array<OrderDTO>), our generator pairs array properties with strict PHPDoc docblocks (/** @var array<UserDTO_OrderItem> */) to ensure static analysis tools like PHPStan and Psalm pass at Level 9 / Level Max.

100% Client-Side Privacy & Air-Gapped Security Guarantee

Generating PHP DTOs from proprietary database structures, internal API responses, or sensitive customer schemas requires complete confidentiality.

JSON Empire guarantees zero data leakage:

Frequently Asked Questions

How do I use this DTO with Spatie Laravel-Data?

Select the "Spatie Laravel-Data DTO" format option in the toolbar. The generated class will extend Spatie\LaravelData\Data, enabling automatic request validation and JSON transformation.

How can I download the generated `.php` file?

Click the "💾 Download .php" button in the workspace panel to save a standalone PHP source file directly to your disk.