Skip to content

Commit d346ca0

Browse files
committed
readme.md: improved
1 parent 5b34f38 commit d346ca0

File tree

1 file changed

+65
-4
lines changed

1 file changed

+65
-4
lines changed

readme.md

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ It results in:
116116
}
117117
```
118118

119+
If the property, constant, method or parameter already exist, it will be overwritten.
120+
119121
PHP Generator supports all new PHP 7.2 features:
120122

121123
```php
@@ -356,19 +358,41 @@ function foo($a)
356358
Namespace
357359
---------
358360

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');
365+
366+
$class = $namespace->addClass('Task');
367+
$interface = $namespace->addInterface('Countable');
368+
$trait = $namespace->addTrait('NameAware');
369+
```
370+
371+
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:
360383

361384
```php
362385
$namespace = new Nette\PhpGenerator\PhpNamespace('Foo');
363386
$namespace->addUse('Bar\AliasedClass');
364387

365388
$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
368391

369392
$method = $class->addMethod('method');
393+
$method->addComment('@return ' . $namespace->unresolveName('Foo\D')); // in comments resolve manually
370394
$method->addParameter('arg')
371-
->setTypeHint('Bar\OtherClass'); // resolves to \Bar\OtherClass
395+
->setTypeHint('Bar\OtherClass'); // it will resolve to \Bar\OtherClass
372396

373397
echo $namespace;
374398
```
@@ -384,12 +408,49 @@ class Demo implements A
384408
{
385409
use AliasedClass;
386410

411+
/**
412+
* @return D
413+
*/
387414
public function method(\Bar\OtherClass $arg)
388415
{
389416
}
390417
}
391418
```
392419

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.');
428+
429+
$namespace = $file->addNamespace('Foo');
430+
$class = $namespace->addClass('A');
431+
$class->addMethod('hello');
432+
433+
echo $file;
434+
```
435+
436+
Result:
437+
438+
```php
439+
<?php
440+
441+
/**
442+
* This file is auto-generated.
443+
*/
444+
445+
namespace Foo;
446+
447+
class A
448+
{
449+
public function hello()
450+
{
451+
}
452+
}
453+
```
393454

394455
Generate using Reflection
395456
-------------------------

0 commit comments

Comments
 (0)