← All tools Code Gen

JSON to PHP Classes

Paste a JSON object and instantly generate fully typed PHP classes. Choose between encapsulated private properties or modern public typed properties — everything runs in your browser.

0 chars

Why Convert JSON to PHP Classes?

Modern PHP development relies heavily on typed classes to model data. Whether you are consuming a REST API, processing webhook payloads, or mapping database rows, working with strongly typed objects instead of raw associative arrays gives you IDE autocompletion, static analysis support, and fewer runtime errors.

Manually writing a PHP class for every JSON structure is tedious and error-prone, especially when the payload contains deeply nested objects. This tool automates the process: paste your JSON, configure your preferences, and get production-ready PHP code in seconds.

Private vs. Public Properties — Which to Choose

# Private + Getters/Setters
  • • Full encapsulation of internal state
  • • Fluent setter chaining with return self
  • • Easy to add validation logic later
  • • Traditional OOP and domain models
$ Public Typed Properties
  • • Concise and readable code
  • • Ideal for DTOs and value objects
  • • PHP 8+ typed properties and readonly
  • • Less boilerplate, faster development

How the Type Mapping Works

The converter inspects each value in your JSON and maps it to the most specific PHP type available. JSON strings become PHP string, integers become int, decimal numbers become float, booleans become bool, and arrays become the array type. Null values are treated as nullable strings with the ? prefix.

When a JSON key contains a nested object, the tool generates a separate PHP class named after that key in PascalCase. This recursive approach handles arbitrarily deep nesting, giving you a complete set of classes that mirror your data structure exactly.

Best Practices for PHP Data Classes

  1. 1

    Use namespaces consistently. Place generated classes under a dedicated namespace like App\DTO or App\Models so they integrate cleanly with your autoloader and do not collide with existing code.

  2. 2

    Leverage constructors for hydration. The constructor-from-array pattern lets you turn a decoded JSON payload into a typed object in a single line: new User(json_decode($json, true)).

  3. 3

    Add validation where needed. Generated code is a starting point. For production use, add type checks, range validations, or integrate with a validation library like Symfony Validator.

  4. 4

    Consider readonly for immutability. In PHP 8.2+, you can mark classes or individual properties as readonly to prevent accidental mutation after construction — ideal for DTOs and API response objects.

  5. 5

    Run static analysis. After generating your classes, run PHPStan or Psalm at a high level to catch type inconsistencies and ensure your code is bulletproof before deployment.

Frequently Asked Questions

How does the converter determine property types?

The converter analyzes each JSON value to infer the corresponding PHP type. Strings map to string, integers to int, decimals to float, booleans to bool, arrays to array, and null values to nullable string (?string). Nested objects are generated as separate PHP classes with their own typed properties.

What is the difference between private and public property styles?

Private properties with getters and setters follow strict encapsulation, with fluent setters returning self for chaining. Public typed properties expose fields directly, which is simpler and often preferred in modern PHP 8+ projects for DTOs and value objects where getter/setter overhead is unnecessary.

How are nested JSON objects handled?

Each nested JSON object is converted into its own PHP class, named after the parent key in PascalCase. For example, an "address" key containing an object generates a separate Address class. This works recursively for any depth of nesting.

What does the constructor from array option do?

When enabled, each generated class includes a __construct(array $data = []) method that populates properties from an associative array. This is useful for hydrating objects from database results, API responses, or decoded JSON payloads. Nested objects are automatically instantiated from their sub-arrays.

Is my JSON data sent to a server?

No. The entire conversion runs locally in your browser using JavaScript. No data is transmitted to any external server. You can verify this by inspecting the network tab in your browser developer tools while generating classes.

Related Tools