Skip to content

Commit

Permalink
Adding example of trait generator
Browse files Browse the repository at this point in the history
Adding example for issue laminas#24
  • Loading branch information
vrkansagara authored Apr 14, 2021
1 parent 4091991 commit 847be56
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions docs/book/generator/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,46 @@ $generator->addMethod(
);
$code = $generator->generate();
```


## Generating Trait

`Laminas\Code\Generator\FileGenerator` can be used to generate the contents of a *PHP* file. You can
include classes as well as arbitrary content body.

```php
use Laminas\Code\Generator\FileGenerator;
use Laminas\Code\Generator\TraitGenerator;

$traitclass = new TraitGenerator();
$traitclass->addMethod('MethodOne');
$traitclass->addConstant('VERSION','1.0.0');

$traitFile = new Laminas\Code\Generator\FileGenerator();
$traitFile->setClass($traitclass);

```

Calling `generate()` will generate the code -- but not write it to a file. You will need to capture
the contents and write them to a file yourself. As an example:

```php
$code = $traitFile->generate();
file_put_contents('Trait.php', $traitclass);
```

The above will generate the following file:

```php
<?php

trait
{
public const VERSION = '1.0.0';

public function MethodOne()
{
}
}

```

0 comments on commit 847be56

Please sign in to comment.