JSON to PHP Converter
Generate PHP classes from JSON data. Paste your JSON:
Loading...
Loading...
Generate PHP Classes from JSON
This tool converts JSON data into PHP class definitions. It supports modern PHP 8+ features like constructor property promotion and typed properties.
Output Options
PHP 8+ Constructor Promotion
Concise syntax with properties declared directly in the constructor:
class User
{
public function __construct(
public int $id,
public string $userName,
public bool $isActive
) {}
}Traditional PHP Class
Classic style with separate properties and constructor:
class User
{
private int $id;
private string $userName;
private bool $isActive;
public function __construct(int $id, string $userName, bool $isActive)
{
$this->id = $id;
$this->userName = $userName;
$this->isActive = $isActive;
}
}Type Mapping
| JSON | PHP |
|---|---|
| string | string |
| integer | int |
| decimal | float |
| boolean | bool |
| null | ?string |
| array | array |
| object | Nested class |
Using Generated Classes
// Create from array
$data = json_decode($jsonString, true);
$user = new User(
$data['id'],
$data['userName'],
$data['isActive']
);
// Access properties
echo $user->userName;With JSON Serialization
// Add JsonSerializable interface
class User implements JsonSerializable
{
// ... properties and constructor ...
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'userName' => $this->userName,
'isActive' => $this->isActive,
];
}
}
// Serialize back to JSON
echo json_encode($user);Related Tools
- JSON Validator — Validate JSON before converting
- JSON to Java — Generate Java POJOs
- JSON to Python — Generate Python dataclasses
- JSON to C# — Generate C# classes
Frequently Asked Questions
What PHP version is required?
Constructor promotion requires PHP 8.0+. Typed properties require PHP 7.4+. Disable these options for older PHP versions.
How do I handle arrays of objects?
PHP doesn't support generic array types in type hints. Arrays are typed as array. Add PHPDoc annotations manually for IDE support:/** @var User[] */