PHP Class Generator
List the properties of a class and get modern PHP: strict types, typed properties, constructor property promotion, readonly classes for value objects and DTOs, getters, setters or immutable withX() methods, toArray and fromArray for converting to and from arrays, and PHPDoc where PHP types are not precise enough, such as list<string>.
- Runs in your browser
- No sign-up
- Free to use
How to use PHP Class Generator
- Enter the class name, namespace and parent or interfaces.
- List the properties as name: type = default.
- Choose promotion, readonly and method options.
- Copy the class into a file named after it.
PHP Class Generator features
PHP 8 syntax
Typed properties, promotion, named arguments, nullsafe operator.
Immutability
readonly classes (8.2) with withX() copy methods.
Array conversion
toArray and fromArray, dates as ISO 8601 strings.
Correct defaults
“now” and array defaults handled the way PHP requires.
PHPDoc
list<T> and array<K, V> types for static analysers.
JsonSerializable
jsonSerialize() added when the interface is listed.
When to use PHP Class Generator
- Creating entities and DTOs for Laravel or Symfony.
- Writing immutable value objects such as Money or Email.
- Modernising PHP 7 classes to PHP 8 style.
- Scaffolding request and response objects for APIs.
PHP Class Generator FAQ
What is constructor property promotion?
A PHP 8.0 feature that declares and assigns a property in the constructor signature: public function __construct(private string $email) {}. It removes most boilerplate.
What does a readonly class do?
In PHP 8.2 and later, every property of a readonly class can be set once, in the constructor, and never changed. It suits value objects and DTOs.
How can I change a readonly object?
With withX() methods that return a new object with one property changed, leaving the original untouched.
Why are some parameters reordered?
PHP 8 deprecates required parameters after optional ones, so parameters with defaults are moved to the end. Named arguments in fromArray make the order irrelevant to callers.
How is a date that defaults to now handled?
PHP parameter defaults must be constant, so the parameter is nullable with default null and the constructor sets new DateTimeImmutable() when nothing is passed.
Is anything uploaded?
No. The class is generated in your browser.
Modern PHP classes
PHP 8 changed how classes are written. Typed properties, constructor property promotion, named arguments, the nullsafe operator and readonly properties together remove most of the boilerplate that older classes needed. The generator writes classes in this modern style while handling the rules that are easy to forget.
Properties are listed with a neutral type notation. string, int, float and bool map directly; date becomes DateTimeImmutable, the immutable and safer date class; any becomes mixed; T[] and map<K, V> become array, with PHPDoc such as list<string> so that PHPStan, Psalm and IDEs know the element types. A question mark after the name makes a property nullable.
Defaults follow PHP’s constraints. Required parameters come first, since PHP 8 deprecates the opposite order. A date that should default to the current time cannot be written as a parameter default, so it becomes a nullable parameter that the constructor fills in. With strict_types enabled, PHP rejects values of the wrong type instead of converting them.
For value objects and DTOs, the readonly option makes the class immutable: PHP 8.2 readonly classes when promotion is used, readonly properties otherwise. Instead of setters, immutable classes get withX() methods that return a modified copy, the pattern used by PSR-7 request objects.
toArray and fromArray convert between objects and arrays, which is what JSON encoding, database rows and request data usually are. Dates are written in ISO 8601 format and parsed back, and fromArray uses named arguments, so it stays correct when parameters are reordered.