You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Generate PHP code, classes, namespaces etc.with a simple programmatical API.
14
+
Do you need to generate PHP code of classes, functions, namespaces, etc.? This library with a friendly API will help you.
15
15
16
-
Documentation can be found on the [website](https://doc.nette.org/php-generator).
16
+
Documentation can be found on the [website](https://doc.nette.org/php-generator). If you like it, **[please make a donation now](https://github.com/sponsors/dg)**. Thank you!
17
17
18
-
If you like Nette, **[please make a donation now](https://nette.org/donate)**. Thank you!
- PhpGenerator 2.6 is compatible with PHP 5.6 to 7.3
34
28
35
29
36
-
Usage
37
-
-----
38
30
39
-
Usage is very easy. Let's start with a straightforward example of generating class:
31
+
Classes
32
+
-------
33
+
34
+
Let's start with a straightforward example of generating class using [ClassType](https://api.nette.org/3.0/Nette/PhpGenerator/ClassType.html):
40
35
41
36
```php
42
37
$class = new Nette\PhpGenerator\ClassType('Demo');
@@ -51,10 +46,6 @@ $class
51
46
52
47
// to generate PHP code simply cast to string or use echo:
53
48
echo $class;
54
-
55
-
// or use printer:
56
-
$printer = new Nette\PhpGenerator\Printer;
57
-
echo $printer->printClass($class);
58
49
```
59
50
60
51
It will render this result:
@@ -72,24 +63,39 @@ final class Demo extends ParentClass implements Countable
72
63
}
73
64
```
74
65
75
-
We can add constants and properties:
66
+
We can also use a printer to generate the code, which, unlike `echo $class`, we will be able to further configure:
76
67
77
68
```php
78
-
$class->addConstant('ID', 123);
69
+
$printer = new Nette\PhpGenerator\Printer;
70
+
echo $printer->printClass($class);
71
+
```
72
+
73
+
We can add constants ([Constant](https://api.nette.org/3.0/Nette/PhpGenerator/Constant.html)) and properties ([Property](https://api.nette.org/3.0/Nette/PhpGenerator/Property.html)):
It can be used also for functions, closures, namespaces etc.
205
169
170
+
Interface or Trait
171
+
------------------
172
+
173
+
You can create interfaces and traits in a similar way, just change the type:
174
+
175
+
```php
176
+
$class = new Nette\PhpGenerator\ClassType('DemoInterface');
177
+
$class->setInterface();
178
+
// or $class->setTrait();
179
+
```
206
180
207
181
Literals
208
182
--------
@@ -235,21 +209,13 @@ class Demo
235
209
}
236
210
```
237
211
238
-
Interface or Trait
239
-
------------------
240
-
241
-
```php
242
-
$class = new Nette\PhpGenerator\ClassType('DemoInterface');
243
-
$class->setInterface();
244
-
// or $class->setTrait();
245
-
```
246
-
247
-
Trait Resolutions and Visibility
248
-
--------------------------------
212
+
Using Traits
213
+
------------
249
214
250
215
```php
251
216
$class = new Nette\PhpGenerator\ClassType('Demo');
252
-
$class->addTrait('SmartObject', ['sayHello as protected']);
217
+
$class->addTrait('SmartObject');
218
+
$class->addTrait('MyTrait', ['sayHello as protected']);
253
219
echo $class;
254
220
```
255
221
@@ -258,7 +224,8 @@ Result:
258
224
```php
259
225
class Demo
260
226
{
261
-
use SmartObject {
227
+
use SmartObject;
228
+
use MyTrait {
262
229
sayHello as protected;
263
230
}
264
231
}
@@ -267,6 +234,8 @@ class Demo
267
234
Anonymous Class
268
235
---------------
269
236
237
+
Give `null` as the name and you have an anonymous class:
238
+
270
239
```php
271
240
$class = new Nette\PhpGenerator\ClassType(null);
272
241
$class->addMethod('__construct')
@@ -289,7 +258,7 @@ $obj = new class ($val) {
289
258
Global Function
290
259
---------------
291
260
292
-
Code of function:
261
+
Code of functions will generate class [GlobalFunction](https://api.nette.org/3.0/Nette/PhpGenerator/GlobalFunction.html):
293
262
294
263
```php
295
264
$function = new Nette\PhpGenerator\GlobalFunction('foo');
@@ -314,7 +283,7 @@ function foo($a, $b)
314
283
Closure
315
284
-------
316
285
317
-
Code of closure:
286
+
Code of closures will generate class [Closure](https://api.nette.org/3.0/Nette/PhpGenerator/Closure.html):
318
287
319
288
```php
320
289
$closure = new Nette\PhpGenerator\Closure;
@@ -337,7 +306,7 @@ function ($a, $b) use (&$c) {
337
306
}
338
307
```
339
308
340
-
Arrow function
309
+
Arrow Function
341
310
--------------
342
311
343
312
You can also print closure as arrow function using printer:
@@ -355,13 +324,32 @@ echo (new Nette\PhpGenerator\Printer)->printArrowFunction($closure);
355
324
Result:
356
325
357
326
```php
358
-
fn ($a, $b) => $a + $b;
327
+
fn ($a, $b) => $a + $b
359
328
```
360
329
361
330
Method and Function Body Generator
362
331
----------------------------------
363
332
364
-
You can use special placeholders for handy way to generate method or function body.
333
+
The body can be passed to the `setBody()` method at once or sequentially (line by line) by repeatedly calling `addBody()`:
334
+
335
+
```php
336
+
$function = new Nette\PhpGenerator\GlobalFunction('foo');
337
+
$function->addBody('$a = rand(10, 20);');
338
+
$function->addBody('return $a;');
339
+
echo $function;
340
+
```
341
+
342
+
Result
343
+
344
+
```php
345
+
function foo()
346
+
{
347
+
$a = rand(10, 20);
348
+
return $a;
349
+
}
350
+
```
351
+
352
+
You can use special placeholders for handy way to inject variables.
365
353
366
354
Simple placeholders:
367
355
@@ -422,16 +410,17 @@ function foo($a)
422
410
Namespace
423
411
---------
424
412
425
-
Classes, traits and interfaces (hereinafter classes) can be grouped into namespaces:
413
+
Classes, traits and interfaces (hereinafter classes) can be grouped into namespaces ([PhpNamespace](https://api.nette.org/3.0/Nette/PhpGenerator/PhpNamespace.html)):
426
414
427
415
```php
428
416
$namespace = new Nette\PhpGenerator\PhpNamespace('Foo');
**IMPORTANT NOTE:** when the class is part of the namespace, it is rendered slightly differently: all types (ie. type hints, return types, parent class name,
439
+
Class Names Resolving
440
+
---------------------
441
+
442
+
**When the class is part of the namespace, it is rendered slightly differently**: all types (ie. type hints, return types, parent class name,
449
443
implemented interfaces and used traits) are automatically *resolved* (unless you turn it off, see below).
450
444
It means that you have to **use full class names** in definitions and they will be replaced
451
445
with aliases (according to the use-statements) or fully qualified names in the resulting code:
0 commit comments