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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-generator

Nette PHP Generator Latest Stable Version Downloads this Month

✅ 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?

Buy me a coffee

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


All versions of php-generator with dependencies

PHP Build Version
Package Version
Requires php Version >=8.0 <8.3
nette/utils Version ^3.2.7 || ^4.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package nette/php-generator contains the following files

Loading the files please wait ....