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
Copy file name to clipboardExpand all lines: readme.md
+65-4Lines changed: 65 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -116,6 +116,8 @@ It results in:
116
116
}
117
117
```
118
118
119
+
If the property, constant, method or parameter already exist, it will be overwritten.
120
+
119
121
PHP Generator supports all new PHP 7.2 features:
120
122
121
123
```php
@@ -356,19 +358,41 @@ function foo($a)
356
358
Namespace
357
359
---------
358
360
359
-
Namespaces may include `use` statements in addition to classes, interfaces, or traits. These statements are used when generating code, so use full class names in the definitions and they will be replace with aliases or fully qualified names in the resulting code.
361
+
Classes, traits and interfaces (hereinafter classes) can be grouped into namespaces:
362
+
363
+
```php
364
+
$namespace = new Nette\PhpGenerator\PhpNamespace('Foo');
If the class already exists, it will be overwritten.
372
+
373
+
You can define use-statements:
374
+
375
+
```php
376
+
$namespace->addUse('Http\Request'); // use Http\Request;
377
+
$namespace->addUse('Http\Request', 'HttpReq'); // use Http\Request as HttpReq;
378
+
```
379
+
380
+
**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,
381
+
implemented interfaces and used traits) are automatically *resolved*. It means that you have to **use full class names** in definitions
382
+
and they will be replaced with aliases (according to the use-statements) or fully qualified names in the resulting code:
360
383
361
384
```php
362
385
$namespace = new Nette\PhpGenerator\PhpNamespace('Foo');
363
386
$namespace->addUse('Bar\AliasedClass');
364
387
365
388
$class = $namespace->addClass('Demo');
366
-
$class->addImplement('Foo\A') // resolves to A
367
-
->addTrait('Bar\AliasedClass'); // resolves to AliasedClass
389
+
$class->addImplement('Foo\A') // it will resolve to A
390
+
->addTrait('Bar\AliasedClass'); // it will resolve to AliasedClass
368
391
369
392
$method = $class->addMethod('method');
393
+
$method->addComment('@return ' . $namespace->unresolveName('Foo\D')); // in comments resolve manually
370
394
$method->addParameter('arg')
371
-
->setTypeHint('Bar\OtherClass'); // resolves to \Bar\OtherClass
395
+
->setTypeHint('Bar\OtherClass'); // it will resolve to \Bar\OtherClass
372
396
373
397
echo $namespace;
374
398
```
@@ -384,12 +408,49 @@ class Demo implements A
384
408
{
385
409
use AliasedClass;
386
410
411
+
/**
412
+
* @return D
413
+
*/
387
414
public function method(\Bar\OtherClass $arg)
388
415
{
389
416
}
390
417
}
391
418
```
392
419
420
+
PHP Files
421
+
---------
422
+
423
+
PHP files can contains multiple classes, namespaces and comments:
424
+
425
+
```php
426
+
$file = new Nette\PhpGenerator\PhpFile;
427
+
$file->addComment('This file is auto-generated.');
0 commit comments