Download the PHP package murtukov/php-code-generator without Composer
On this page you can find all versions of the php package murtukov/php-code-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download murtukov/php-code-generator
More information about murtukov/php-code-generator
Files in murtukov/php-code-generator
Package php-code-generator
Short Description A library to generate php code
License MIT
Informations about the package php-code-generator
PHPCodeGenerator
A library to generate PHP 7.4 code
- Installation
- File
- OOP
- Class
- Interface
- Trait
- Functions
- Function
- Method
- Constructor Property Promotion
- Closure
- Arrow Function
- Object Instantiation
- Arrays
- Control structures
- IfElse
- Loops
- Comments
- Namespaces
- Literal
- Global Configs
Installation
Components
File
Result:
Class
Result:
Interface
Result:
Trait
Result:
Function
Result:
Method
Result:
Constructor property promotion
Result:
Same but creating from a class object:
Modifiers can also be set directly on argument objects:
Closure
Result:
Arrow Function
Result:
Object Instantiation
Result:
You can prevent shortening of the class qualifier by prefixing its name with @
symbol.
Result:
The @
suppress symbol can by changed with static config class.
Arrays
This library provides a useful tool to stringify variables: Utils::stringify()
. It is similar to
the var_export
function, but with more control over arrays formatting. Arrays can also be wrapped
by the Collection
class, which internally use Utils::stringify()
.
Arrays with 0
-key defined are considered "numeric" and are stringified inline and without keys by default.
Result:
All other arrays are stringified multiline and with keys:
Result:
If you want to define yourself, how arrays are stringified, simply wrap them into Collection
class:
Result:
assoc
collection example:
Result:
The Collection
class applies its formatting rules only to the top level array, using default formatting for
all nested arrays:
Result:
If ... else
Result:
Loops
Results:
Comments
Result:
Namespaces
PhpFile
component automatically resolves class qualifiers from all its child components during the rendering.
Result:
However class qualifiers are NOT resolved from scalaras and arrays, unless wrapped in special objects:
Result:
You can always add use statements manually by calling $file->addUse()
or $file->addUseGroup()
:
Result:
Although all components of this library implement the magic __toString()
method, avoid concatenating
them, as it will convert them into string scalars and all class qualifiers will be lost.
So instead of concatenation:
pass parts as separate arguments, as append
is a variadic function:
Literal
Generates code literally as provided (without any additional processing)
Result:
The string can hold placeholders similar to sprintf
function:
Result:
Escaping the reserved %
char:
Result:
Global Configs
All global configs are stored as static properties in the Config
class.
Indent
Default indent contains 4 spaces. You can change it by rewriting the $indent
property:
Shorten qualifiers
As mentioned in the Namespaces section, class qualifiers are shortened automatically and added
to the top of PhpFile
output. In order to disable it overwrite the $shortenQualifiers
property:
Suppress symbol
As mentioned in the Object Instantiation section, you can suppress shortening of
class qualifiers by prefixing them with the @
symbol. In order to change this symbol, overwrite the
$suppressSymbol
property:
All versions of php-code-generator with dependencies
ext-json Version *