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.
Download nette/php-generator
More information about nette/php-generator
Files in nette/php-generator
Package 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
Homepage https://nette.org
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
Installation
For PHP compatibility, see the library's website.
Support Me
Do you like PHP Generator? Are you looking forward to the new features?
Thank you!
Classes
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):
It generates:
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 setReadOnly()
.
If the added property, constant, method or parameter already exist, it throws exception.
Members can be removed using removeProperty()
, removeConstant()
, removeMethod()
or removeParameter()
.
You can also add existing Method
, Property
or Constant
objects to the class:
You can clone existing methods, properties and constants with a different name using cloneWithName()
:
Interface or Trait
You can create interfaces and traits (classes InterfaceType and TraitType):
Using traits:
Result:
Enums
You can easily create the enums that PHP 8.1 brings (class EnumType):
Result:
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 addComment()
or addAttribute()
.
Anonymous Class
Give null
as the name and you have an anonymous class:
Result:
Global Function
Code of functions will generate class GlobalFunction:
Result:
Closure
Code of closures will generate class Closure:
Result:
Arrow Function
You can also print closure as arrow function using printer:
Result:
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 setVariadics()
:
Generates:
Method and Function Body
The body can be passed to the setBody()
method at once or sequentially (line by line) by repeatedly calling addBody()
:
Result
You can use special placeholders for handy way to inject variables.
Simple placeholders ?
Result:
Variadic placeholder ...?
Result:
You can also use PHP 8 named parameters using placeholder ...?:
Escape placeholder using slash \?
Result:
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:
Types
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 setReturnType()
.
Literals
With Literal
you can pass arbitrary PHP code to, for example, default property or parameter values etc:
Result:
You can also pass parameters to Literal
and have it formatted into valid PHP code using special placeholders:
Attributes
You can add PHP 8 attributes to all classes, methods, properties, constants, enum cases, functions, closures and parameters (class Attribute).
Result:
Namespace
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 simplifyName
method:
Conversely, you can convert a simplified class, function or constant name to a fully qualified one using the resolveName
method:
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:
Result:
Auto-resolving can be turned off this way:
PHP Files
Classes, functions and namespaces can be grouped into PHP files represented by the class PhpFile:
Result:
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
(it requires 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:
This requires nikic/php-parser
to be installed.
Variables Dumper
The Dumper returns a parsable PHP string representation of a variable. Provides better and clearer output that native functon var_export()
.
Compatibility Table
- 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