Download the PHP package nette/php-generator without Composer
On this page you can find all versions of the php package nette/php-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
More information about nette/php-generator
Files in nette/php-generator
Short Description 🐘 Nette PHP Generator: generates neat PHP code for you. Supports new PHP 8.2 features.
License BSD-3-Clause GPL-2.0-only GPL-3.0-only
Informations about the package php-generator
Nette PHP Generator
✅ Need to generate PHP code for PHP files, etc.?
✅ Supports all the latest PHP features like attributes, etc.
✅ Allows you to easily modify existing classes
✅ PSR-12 compliant output
✅ Highly mature, stable, and widely used library
For PHP compatibility, see the library's website.
Do you like PHP Generator? Are you looking forward to the new features?
Let's start with a straightforward example of generating class using ClassType:
It will render this result:
We can also use a printer to generate the code, which, unlike
echo $class, we will be able to further configure:
We can add constants (class Constant) and properties (class Property):
And we can add methods:
It results in:
Promoted parameters introduced by PHP 8.0 can be passed to the constructor:
It results in:
Readonly properties and classes can be marked via
If the added property, constant, method or parameter already exist, it throws exception.
Members can be removed using
You can also add existing
Constant objects to the class:
You can clone existing methods, properties and constants with a different name using
Interface or Trait
You can create interfaces and traits (classes InterfaceType and TraitType):
You can easily create the enums that PHP 8.1 brings (class EnumType):
You can also define scalar equivalents for cases to create a backed enum:
It is possible to add a comment or attributes to each case using
null as the name and you have an anonymous class:
Code of functions will generate class GlobalFunction:
Code of closures will generate class Closure:
You can also print closure as arrow function using printer:
Method and Function Signature
Methods are represented by the class Method. You can set visibility, return value, add comments, [attributes|#Attributes] etc:
Each parameter is represented by a class Parameter. Again, you can set every conceivable property:
To define the so-called variadics parameters (or also the splat, spread, ellipsis, unpacking or three dots operator), use
Method and Function Body
The body can be passed to the
setBody() method at once or sequentially (line by line) by repeatedly calling
You can use special placeholders for handy way to inject variables.
You can also use PHP 8 named parameters using placeholder
Escape placeholder using slash
Printers and PSR compliance
PHP code is generated by
Printer objects. There is a
PsrPrinter whose output conforms to PSR-2 and PSR-12 and uses spaces for indentation, and a
Printer that uses tabs for indentation.
Need to customize printer behavior? Create your own by inheriting the
Printer class. You can reconfigure these variables:
Each type or union/intersection type can be passed as a string, you can also use predefined constants for native types:
The same applies to the method
Literal you can pass arbitrary PHP code to, for example, default property or parameter values etc:
You can also pass parameters to
Literal and have it formatted into valid PHP code using special placeholders:
You can add PHP 8 attributes to all classes, methods, properties, constants, enum cases, functions, closures and parameters (class Attribute).
Classes, traits, interfaces and enums (hereinafter classes) can be grouped into namespaces (class PhpNamespace):
If the class already exists, it throws exception.
You can define use-statements:
To simplify a fully qualified class, function or constant name according to the defined aliases, use the
Conversely, you can convert a simplified class, function or constant name to a fully qualified one using the
Class Names Resolving
When the class is part of the namespace, it is rendered slightly differently: all types (ie. type hints, return types, parent class name, implemented interfaces, used traits and attributes) are automatically resolved (unless you turn it off, see below). It means that you have to use full class names in definitions and they will be replaced with aliases (according to the use-statements) or fully qualified names in the resulting code:
Auto-resolving can be turned off this way:
Classes, functions and namespaces can be grouped into PHP files represented by the class PhpFile:
Generating According to Existing Ones
In addition to being able to model classes and functions using the API described above, you can also have them automatically generated using existing ones:
Function and method bodies are empty by default. If you want to load them as well, use this way
nikic/php-parser to be installed):
Loading from PHP File
You can also load classes and functions directly from a PHP file that is not already loaded or string of PHP code:
Loading the entire PHP file, which may contain multiple classes or even multiple namespaces:
nikic/php-parser to be installed.
The Dumper returns a parsable PHP string representation of a variable. Provides better and clearer output that native functon
- PhpGenerator 4.0 is compatible with PHP 8.0 to 8.2
- PhpGenerator 3.6 is compatible with PHP 7.2 to 8.2
- PhpGenerator 3.2 – 3.5 is compatible with PHP 7.1 to 8.0
- PhpGenerator 3.1 is compatible with PHP 7.1 to 7.3
- PhpGenerator 3.0 is compatible with PHP 7.0 to 7.3
- PhpGenerator 2.6 is compatible with PHP 5.6 to 7.3
All versions of php-generator with dependencies
nette/utils Version ^3.2.7 || ^4.0