@@ -51,6 +51,10 @@ $class
51
51
52
52
// to generate PHP code simply cast to string or use echo:
53
53
echo $class;
54
+
55
+ // or use printer:
56
+ $printer = new Nette\PhpGenerator\Printer;
57
+ echo $printer->printClass($class);
54
58
```
55
59
56
60
It will render this result:
@@ -82,10 +86,10 @@ $class->addProperty('items', [1, 2, 3])
82
86
It generates:
83
87
84
88
``` php
85
- const ID = 123;
89
+ const ID = 123;
86
90
87
- /** @var int[] */
88
- private static $items = [1, 2, 3];
91
+ /** @var int[] */
92
+ private static $items = [1, 2, 3];
89
93
```
90
94
91
95
And we can add methods with parameters:
@@ -106,14 +110,14 @@ $method->addParameter('items', []) // $items = []
106
110
It results in:
107
111
108
112
``` php
109
- /**
110
- * Count it.
111
- * @return int
112
- */
113
- final protected function count(array & $items = [])
114
- {
115
- return count($items ?: $this->items);
116
- }
113
+ /**
114
+ * Count it.
115
+ * @return int
116
+ */
117
+ final protected function count(array & $items = [])
118
+ {
119
+ return count($items ?: $this->items);
120
+ }
117
121
```
118
122
119
123
If the property, constant, method or parameter already exist, it will be overwritten.
@@ -181,11 +185,10 @@ Tabs versus spaces
181
185
The generated code uses tabs for indentation. If you want to have the output compatible with PSR-2 or PSR-12, use ` PsrPrinter ` :
182
186
183
187
``` php
184
- $printer = new Nette\PhpGenerator\PsrPrinter;
185
-
186
188
$class = new Nette\PhpGenerator\ClassType('Demo');
187
189
// ...
188
190
191
+ $printer = new Nette\PhpGenerator\PsrPrinter;
189
192
echo $printer->printClass($class); // 4 spaces indentation
190
193
```
191
194
0 commit comments