🐘 Nette PHP Generator: generates neat PHP code for you. Supports new PHP 8.1 features.

Installs: 21 071 296

Dependents: 309

Suggesters: 3

Security: 0

Stars: 1 427

Watchers: 61

Forks: 84

Open Issues: 0

v3.6.2 2021-09-23 21:48 UTC


Downloads this Month Tests Coverage Status Latest Stable Version License


Do you need to generate PHP code of classes, functions, namespaces, etc.? This library with a friendly API will help you.

Documentation can be found on the website.

Support Me

Do you like PHP Generator? Are you looking forward to the new features?

Buy me a coffee

Thank you!


composer require nette/php-generator
  • PhpGenerator 4.0 is compatible with PHP 8.0 to 8.1
  • PhpGenerator 3.6 is compatible with PHP 7.2 to 8.1
  • 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


Let's start with a straightforward example of generating class using ClassType:

$class = new Nette\PhpGenerator\ClassType('Demo');

	->addComment("Description of class.\nSecond line\n")
	->addComment('@property-read Nette\Forms\Form $form');

// to generate PHP code simply cast to string or use echo:
echo $class;

It will render this result:

 * Description of class.
 * Second line
 * @property-read Nette\Forms\Form $form
final class Demo extends ParentClass implements Countable

We can also use a printer to generate the code, which, unlike echo $class, we will be able to further configure:

$printer = new Nette\PhpGenerator\Printer;
echo $printer->printClass($class);

We can add constants (Constant) and properties (Property):

$class->addConstant('ID', 123)
	->setProtected() // constant visiblity

$class->addProperty('items', [1, 2, 3])
	->setPrivate() // or setVisibility('private')
	->addComment('@var int[]');

	->setInitialized(); // prints '= null'

It generates:

final protected const ID = 123;

/** @var int[] */
private static $items = [1, 2, 3];

public ?array $list = null;

And we can add methods with parameters:

$method = $class->addMethod('count')
	->addComment('Count it.')
	->addComment('@return int')
	->setReturnType('int') // method return type
	->setReturnNullable() // nullable return type
	->setBody('return count($items ?: $this->items);');

$method->addParameter('items', []) // $items = []
		->setReference()           // &$items = []
		->setType('array');        // array &$items = []

It results in:

 * Count it.
 * @return int
final protected function count(array &$items = []): ?int
	return count($items ?: $this->items);

Promoted parameters introduced by PHP 8.0 can be passed to the constructor:

$method = $class->addMethod('__construct');
$method->addPromotedParameter('args', [])

It results in:

public function __construct(
	public $name,
	private $args = [],
) {

Readonly properties introduced by PHP 8.1 can be marked via setReadOnly().

If the added property, constant, method or parameter already exist, it will be overwritten.

Members can be removed using removeProperty(), removeConstant(), removeMethod() or removeParameter().

You can also add existing Method, Property or Constant objects to the class:

$method = new Nette\PhpGenerator\Method('getHandle');
$property = new Nette\PhpGenerator\Property('handle');
$const = new Nette\PhpGenerator\Constant('ROLE');

$class = (new Nette\PhpGenerator\ClassType('Demo'))

You can clone existing methods, properties and constants with a different name using cloneWithName():

$methodCount = $class->getMethod('count');
$methodRecount = $methodCount->cloneWithName('recount');


Each type or union/intersection type can be passed as a string, you can also use predefined constants for native types:

use Nette\PhpGenerator\Type;

$member->setType('array'); // or Type::ARRAY;
$member->setType('array|string'); // or Type::union('array', 'string')
$member->setType('Foo&Bar'); // or Type::intersection(Foo::class, Bar::class)
$member->setType(null); // removes type

The same applies to the method setReturnType().

Tabs versus Spaces

The generated code uses tabs for indentation. If you want to have the output compatible with PSR-2 or PSR-12, use PsrPrinter:

$class = new Nette\PhpGenerator\ClassType('Demo');
// ...

$printer = new Nette\PhpGenerator\PsrPrinter;
echo $printer->printClass($class); // 4 spaces indentation

Interface or Trait

You can create interfaces and traits:

$interface = Nette\PhpGenerator\ClassType::interface('MyInterface');
$trait = Nette\PhpGenerator\ClassType::trait('MyTrait');
// in a similar way $class = Nette\PhpGenerator\ClassType::class('MyClass');


You can easily create the enums that PHP 8.1 brings:

$enum = Nette\PhpGenerator\ClassType::enum('Suit');

echo $enum;


enum Suit
        case Clubs;
        case Diamonds;
        case Hearts;
        case Spades;

You can also define scalar equivalents for cases to create a backed enum:

$enum->addCase('Clubs', '♣');
$enum->addCase('Diamonds', '♦');

It is possible to add a comment or attributes to each case using addComment() or addAttribute().


With Literal you can pass arbitrary PHP code to, for example, default property or parameter values etc:

use Nette\PhpGenerator\Literal;

$class = new Nette\PhpGenerator\ClassType('Demo');

$class->addProperty('foo', new Literal('Iterator::SELF_FIRST'));

	->addParameter('id', new Literal('1 + 2'));

echo $class;


class Demo
	public $foo = Iterator::SELF_FIRST;

	public function bar($id = 1 + 2)

You can also pass parameters to Literal and have it formatted into valid PHP code using special placeholders:

new Literal('substr(?, ?)', [$a, $b]);
// generates, for example: substr('hello', 5);

Using Traits

$class = new Nette\PhpGenerator\ClassType('Demo');
	->addResolution('sayHello as protected')
	->addComment('@use MyTrait<Foo>');
echo $class;


class Demo
	use SmartObject;
	/** @use MyTrait<Foo> */
	use MyTrait {
		sayHello as protected;

Anonymous Class

Give null as the name and you have an anonymous class:

$class = new Nette\PhpGenerator\ClassType(null);

echo '$obj = new class ($val) ' . $class . ';';


$obj = new class ($val) {

	public function __construct($foo)

Global Function

Code of functions will generate class GlobalFunction:

$function = new Nette\PhpGenerator\GlobalFunction('foo');
$function->setBody('return $a + $b;');
echo $function;

// or use PsrPrinter for output compatible with PSR-2 / PSR-12
// echo (new Nette\PhpGenerator\PsrPrinter)->printFunction($function);


function foo($a, $b)
	return $a + $b;


Code of closures will generate class Closure:

$closure = new Nette\PhpGenerator\Closure;
$closure->setBody('return $a + $b;');
echo $closure;

// or use PsrPrinter for output compatible with PSR-2 / PSR-12
// echo (new Nette\PhpGenerator\PsrPrinter)->printClosure($closure);


function ($a, $b) use (&$c) {
	return $a + $b;

Arrow Function

You can also print closure as arrow function using printer:

$closure = new Nette\PhpGenerator\Closure;
$closure->setBody('$a + $b');

// or use PsrPrinter for output compatible with PSR-2 / PSR-12
echo (new Nette\PhpGenerator\Printer)->printArrowFunction($closure);


fn($a, $b) => $a + $b


You can add PHP 8 attributes to all classes, methods, properties, constants, enum cases, functions, closures and parameters.

$class = new Nette\PhpGenerator\ClassType('Demo');

	->addAttribute('WithArguments', [1, 2]);

$method = $class->addMethod('count')
	->addAttribute('Foo\Cached', ['mode' => true]);


echo $class;


class Demo
	#[WithArguments(1, 2)]
	public $list;

	#[Foo\Cached(mode: true)]
	public function count(#[Bar] $items)

Method and Function Body Generator

The body can be passed to the setBody() method at once or sequentially (line by line) by repeatedly calling addBody():

$function = new Nette\PhpGenerator\GlobalFunction('foo');
$function->addBody('$a = rand(10, 20);');
$function->addBody('return $a;');
echo $function;


function foo()
	$a = rand(10, 20);
	return $a;

You can use special placeholders for handy way to inject variables.

Simple placeholders ?

$str = 'any string';
$num = 3;
$function = new Nette\PhpGenerator\GlobalFunction('foo');
$function->addBody('return substr(?, ?);', [$str, $num]);
echo $function;


function foo()
	return substr('any string', 3);

Variadic placeholder ...?

$items = [1, 2, 3];
$function = new Nette\PhpGenerator\GlobalFunction('foo');
$function->setBody('myfunc(...?);', [$items]);
echo $function;


function foo()
	myfunc(1, 2, 3);

You can also use PHP 8 named parameters using placeholder ...?:

$items = ['foo' => 1, 'bar' => true];
$function->setBody('myfunc(...?:);', [$items]);

// myfunc(foo: 1, bar: true);

Escape placeholder using slash \?

$num = 3;
$function = new Nette\PhpGenerator\GlobalFunction('foo');
$function->addBody('return $a \? 10 : ?;', [$num]);
echo $function;


function foo($a)
	return $a ? 10 : 3;


Classes, traits, interfaces and enums (hereinafter classes) can be grouped into namespaces (PhpNamespace):

$namespace = new Nette\PhpGenerator\PhpNamespace('Foo');

// create new classes in the namespace
$class = $namespace->addClass('Task');
$interface = $namespace->addInterface('Countable');
$trait = $namespace->addTrait('NameAware');

// or insert an existing class into the namespace
$class = new Nette\PhpGenerator\ClassType('Task');

If the class already exists, it will be overwritten.

You can define use-statements:

// use Http\Request;
// use Http\Request as HttpReq;
$namespace->addUse(Http\Request::class, 'HttpReq');

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:

$namespace = new Nette\PhpGenerator\PhpNamespace('Foo');

$class = $namespace->addClass('Demo');
$class->addImplement('Foo\A') // it will resolve to A
	->addTrait('Bar\AliasedClass'); // it will resolve to AliasedClass

$method = $class->addMethod('method');
$method->addComment('@return ' . $namespace->simplifyType('Foo\D')); // in comments resolve manually
	->setType('Bar\OtherClass'); // it will resolve to \Bar\OtherClass

echo $namespace;

// or use PsrPrinter for output compatible with PSR-2 / PSR-12
// echo (new Nette\PhpGenerator\PsrPrinter)->printNamespace($namespace);


namespace Foo;

use Bar\AliasedClass;

class Demo implements A
	use AliasedClass;

	 * @return D
	public function method(\Bar\OtherClass $arg)

Auto-resolving can be turned off this way:

$printer = new Nette\PhpGenerator\Printer; // or PsrPrinter
echo $printer->printNamespace($namespace);

PHP Files

Classes, functions and namespaces can be grouped into PHP files represented by the class PhpFile:

$file = new Nette\PhpGenerator\PhpFile;
$file->addComment('This file is auto-generated.');
$file->setStrictTypes(); // adds declare(strict_types=1)

$class = $file->addClass('Foo\A');
$function = $file->addFunction('Foo\foo');

// or
// $namespace = $file->addNamespace('Foo');
// $class = $namespace->addClass('A');
// $function = $namespace->addFunction('foo');

echo $file;

// or use PsrPrinter for output compatible with PSR-2 / PSR-12
// echo (new Nette\PhpGenerator\PsrPrinter)->printFile($file);



 * This file is auto-generated.


namespace Foo;

class A

function foo()

Generate using Reflection

Another common use case is to create class or function based on existing one:

$class = Nette\PhpGenerator\ClassType::from(PDO::class);

$function = Nette\PhpGenerator\GlobalFunction::from('trim');

$closure = Nette\PhpGenerator\Closure::from(
	function (stdClass $a, $b = null) {}

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):

$class = Nette\PhpGenerator\ClassType::from(PDO::class, withBodies: true);

$function = Nette\PhpGenerator\GlobalFunction::from('dump', withBody: true);

Load class from file

You can also load classes directly from a PHP file that is not already loaded or string of PHP code:

$class = Nette\PhpGenerator\ClassType::fromCode(<<<XX

	class Demo
		public $foo;

Loading the entire PHP file, which may contain multiple classes or even multiple namespaces:

$file = Nette\PhpGenerator\PhpFile::fromCode(file_get_contents('classes.php'));

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().

$dumper = new Nette\PhpGenerator\Dumper;

$var = ['a', 'b', 123];

echo $dumper->dump($var); // prints ['a', 'b', 123]

Custom Printer

Need to customize printer behavior? Create your own by inheriting the Printer class. You can reconfigure these variables:

class MyPrinter extends Nette\PhpGenerator\Printer
	public int $wrapLength = 120;
	public string $indentation = "\t";
	public int $linesBetweenProperties = 0;
	public int $linesBetweenMethods = 2;
	public string $returnTypeColon = ': ';