Nette PhpGenerator
A PHP library that generates neat PHP classes, functions, and files programmatically.
Repository Health
Technical Analysis
Nette PhpGenerator is a mature, widely used PHP library for generating PHP code from your program. It can produce classes, interfaces, traits, enums, functions, and complete PHP files with a fluent object API, so you never have to concatenate source strings by hand.
It supports the latest PHP language features including property hooks, enums, and attributes, emits PSR-12 / PER-compliant output, and can even reverse-engineer existing classes so you can inspect and modify them. It works on PHP 8.1 through 8.5.
What You Get
- Fluent builders for classes, interfaces, traits, enums, and anonymous classes
- Generation of global functions, closures, and complete PHP files with namespaces
- Support for modern PHP features: property hooks, enums, attributes, readonly, and typed properties
- A configurable Printer that produces PSR-12 / PER-compliant output
- Reverse engineering to build generator objects from existing classes
Common Use Cases
- Scaffolding boilerplate classes in framework tooling and CLI generators
- Emitting compiled or cached PHP (DI containers, proxies, mappers)
- Building code-generation steps in ORMs and API client generators
- Programmatically refactoring or extending existing classes
Under The Hood
Architecture
The library models PHP constructs as an object graph under the Nette\PhpGenerator namespace: ClassType, InterfaceType, TraitType, and EnumType hold collections of Method, Property, Constant, and Parameter objects, each configured through chainable setters. A PhpFile groups multiple types with namespaces and use-statements. Emitting code is delegated to a Printer (or PSR printer) that walks the object graph and renders formatted source, cleanly separating the in-memory model from output formatting.
Tech Stack
100% PHP with no runtime dependencies beyond the standard library, distributed via Composer as nette/php-generator. It targets PHP 8.1 to 8.5 and tracks new language features closely. Tests use the Nette Tester framework and CI runs on GitHub Actions.
Code Quality
The project is highly mature: 70+ releases since 2014, active maintenance led by Nette author David Grudl, and an extensive test suite. The single-namespace src/PhpGenerator layout is well organized, and output is validated against PSR-12/PER coding standards, reflecting strong attention to correctness of generated code.
API Design
The fluent API is highly ergonomic: (new ClassType('Demo'))->setFinal()->setExtends(...)->addImplement(...) reads naturally, and simply echoing an object emits its code. Reverse engineering from existing classes and a configurable printer give advanced control without complicating the common path. Comprehensive documentation on the Nette site keeps the learning curve low.