Library / SDK
nette/php-generator avatar
nette/php-generator

Nette PhpGenerator builds classes, enums and files through setter chains and throws on duplicate members

🐘 Generates neat PHP code for you. Supports new PHP 8.5 features.

2,255 stars144 forksPHPLicense varies

At a glance

What is it?
nette/php-generator is a Composer library that writes PHP source from objects such as ClassType and EnumType. It prints PSR-12 style output, and the member-level API is strict about duplicates.
Who is it for?
Choose nette/php-generator when a build step or scaffolder has to emit PHP classes, enums, traits or whole files and you want the output to follow PSR-12 or PER style. Skip it for one-off snippets that a heredoc handles.
Can I use it commercially?
Check first. The repository uses a licence we do not classify automatically, so read its LICENSE file before any commercial use.
Is it still maintained?
Yes. The repository last received commits 20 days ago.
What is it written in?
Mainly PHP, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 25, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • nette/php-generator2,255 ★ · -4
Star history
Repository2026-10-122026-11-25
nette/php-generator2.3k2.3k

-4 Stars in the last 44 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

ClassType turns a chain of setters into a class declaration

Everything starts with an object. You create a `Nette\PhpGenerator\ClassType('Demo')`, call `setFinal()`, `setExtends(ParentClass::class)` and `addImplement(Countable::class)`, then `echo $class`. Typecasting to string is the whole rendering step, and the output is a docblock plus `final class Demo extends ParentClass implements Countable` with an empty body.

Comments go through `addComment()`, and the README example passes a string with embedded newlines to get a multi-line description followed by a `@property-read` tag. The same style of object exists for interfaces and traits: `InterfaceType` and `TraitType` take just a name.

Pass `null` as the class name and you get an anonymous class instead. You then wrap the printed body yourself, for instance by concatenating `'$obj = new class ($val) '` with the class and a closing semicolon. The library prints the braces and members, not the `new class` expression around them.

Constants, properties and methods carry their own setters

Members are added with `addConstant()`, `addProperty()` and `addMethod()`, and each returns an object you keep configuring. A constant can be made `setProtected()`, given `setType('int')` and marked `setFinal()`, which prints as `final protected const int ID = 123;`. A property added with `addProperty('list')` and `setType('?array')` prints with `= null` only after you call `setInitialized()`.

Methods take a body as a plain string through `setBody()`. The README example passes `'return count($items ?: $this->items);'`, so the generator does not parse or check that PHP. A syntax error in the string ends up in the output untouched.

Parameters come from `addParameter()`, with `setReference()` and `setType('array')` producing `array &$items = []`. Constructor promotion has its own call, `addPromotedParameter()`, which prints `public $name` and `private $args = []` inside the constructor signature. Readonly properties and classes use `setReadOnly()`.

Adding a member that already exists throws an exception

If an added property, constant, method or parameter already exists, an exception is thrown. That makes accidental overwrites loud, and it also means a script that regenerates the same class twice in one process has to guard against it or remove the old member first.

Removal has one method per member kind: `removeProperty()`, `removeConstant()`, `removeMethod()` and `removeParameter()`. Whole objects can be attached with `addMember()`, so a `Method`, `Property` or `Constant` built separately can join a class afterwards.

Copying has a helper as well. `cloneWithName()` duplicates an existing method, property or constant under a new name, and the README shows `$class->getMethod('count')` cloned to `recount` and then added with `addMember()`. The clone is not added for you.

Enums distinguish pure cases from backed cases by the values you give

`EnumType` covers PHP 8.1 enums. Calling `addCase('Clubs')` with no value gives a pure case. Calling `addCase()` with a second argument, a string value for the case, makes the enum backed, and the printed header becomes `enum Suit: string`.

You do not state the backing type in that example. It is inferred from the case values, or you can set it explicitly with `setType()`. Each case can carry a comment through `addComment()` or an attribute through `addAttribute()`.

The feature list at the top of the README also names property hooks and attributes, and links sections for both. The installation note says PhpGenerator 5.0 is compatible with PHP 8.3 to 8.6, so generated hooks and similar syntax only make sense on a runtime in that range, and the code you emit has to run on whatever PHP you deploy it to.

Echo uses defaults, and a Printer is for the output you must tune

Casting an object to a string is the short path. For control over the output you create `Nette\PhpGenerator\Printer` and call `printClass($class)`. The README says a printer, unlike `echo $class`, can be further configured, and points to a section on the printer and PSR compliance.

The headline claim is output compliant with PSR-12 or PER coding style. That is a formatting promise about the generated text, and the generator still leaves what the code does to you. The `addPromotedParameter()` example prints a trailing comma after the last parameter and an empty constructor body.

The README also states that existing classes can be modified, and links a section titled generating from existing ones. This article does not cover how that works.

Install with Composer and check the version line before you pin

Installation is one command:

shell
composer require nette/php-generator

The README says PhpGenerator 5.0 is compatible with PHP 8.3 to 8.6. The recent GitHub releases are numbered differently: v4.2.4 on 26 October 2026, v4.2.3 on 22 October 2026 and v4.2.2 on 9 February 2026. The repository's last push was on 22 October 2026.

The mismatch between a 5.0 compatibility note and 4.2.x tags is something the README does not explain. Pin the constraint you actually install and read the Composer output rather than assuming the version in the prose.

The documentation lives on the project's site at doc.nette.org/php-generator, and the API reference is hosted at api.nette.org/php-generator.

Licence metadata is blank, so license.md is the only source for the terms

The repository reports its licence as NOASSERTION, and the top level holds a `license.md` file. The library does not tell you the terms in the metadata, so open that file yourself before shipping it inside a product.

The repository also contains `AGENTS.md`, `composer.json`, `phpstan.neon`, `dresscode.neon`, a `docs/` directory, `src/` and `tests/`. The README does not explain what `dresscode.neon` configures.

The README's feature list links sections on PHP files, property hooks, attributes and printer configuration. Details on those are not covered here, so read the library's own documentation for them before you plan around them.

Editorial conclusion

Choose nette/php-generator when a build step or scaffolder has to emit PHP classes, enums, traits or whole files and you want the output to follow PSR-12 or PER style. Skip it for one-off snippets that a heredoc handles. Before adopting it, check the PHP version line in the README against your runtime, since the installation note names PhpGenerator 5.0 and PHP 8.3 to 8.6, and read license.md, because the repository metadata lists the licence only as NOASSERTION.

Frequently asked questions

What does nette/php-generator do?

It generates PHP code for classes, functions and complete PHP files from objects such as ClassType, with output that follows PSR-12 or PER coding style.

How do I install nette/php-generator?

Run composer require nette/php-generator. The README says PhpGenerator 5.0 is compatible with PHP 8.3 to 8.6.

What happens if nette/php-generator is asked to add a member twice?

If an added property, constant, method or parameter already exists, an exception is thrown. Members can be removed with removeProperty(), removeConstant(), removeMethod() or removeParameter().

Can nette/php-generator create enums?

Yes. EnumType creates pure and backed enums, and the backing type is inferred from the case values or set with setType().

Official sources

  1. Issues
  2. nette/php-generator on GitHub
  3. Project website
  4. README
  5. Releases

Was this useful?