PHPackages                             drewlabs/code-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. drewlabs/code-generator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

drewlabs/code-generator
=======================

Simple utility classes for generating PHP classes, methods, properties and interfaces

v0.3.21(1y ago)01842MITPHPPHP &gt;=7.2

Since Mar 22Pushed 1y agoCompare

[ Source](https://github.com/azandrew-sidoine/drewlabs-php-code-generator)[ Packagist](https://packagist.org/packages/drewlabs/code-generator)[ RSS](/packages/drewlabs-code-generator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (19)Used By (2)

```
# PHP Class Generator

The PHP component generator provides implementations for generating PHP classes, interfaces, Traits, Methods, Properties code from configured classes.

## Usage

* Creating a class Property

```php
//...
use Drewlabs\CodeGenerator\Models\PHPClassProperty;
use Drewlabs\CodeGenerator\Types\PHPTypesModifiers;

// ...

// Creates a property providing all definition in the constructor
$property = new PHPClassProperty($name, $type, $modifier, $value, $description);

// We can also create a property and set the definitions later
$property = (new PHPClassProperty($name))
    ->setModifier($modifier ?? PHPTypesModifiers::PUBLIC)
    ->addComment([$description])
    ->value($value);

// Converting the Property to it string representation
$code = $property->__toString();
```

* Creating a const class property

```php
// Simply calling asConstant on the default PHPClassProperty class object convert the property to a constant property
$property = (new PHPClassProperty($name))
    ->asConstant()
    ->setModifier($modifier ?? PHPTypesModifiers::PUBLIC)
    ->addComment([$description, 'This is a second line comment'])
    ->value($value);

// Converting the Property to it string representation
return $property->__toString();
```

* Creating a class method

```php
// ...
use Drewlabs\CodeGenerator\Models\PHPClassMethod;
use Drewlabs\CodeGenerator\Models\PHPFunctionParameter;
use RuntimeException;

// ...
$method = (new PHPClassMethod('__construct'))->throws([
        RuntimeException::class
    ])->addParam(new PHPFunctionParameter('users', null, ["user1" => "Sandra", "user2" => "Emily"]))
        ->addParam((new PHPFunctionParameter('params', PHPFunctionParameter::class))->asOptional())
        ->addContents(
            addLine('// This is an extra line')
        ->setReturnType(Stringable::class)
        ->addComment('This is a PHP Class method');

// Method definition to string
$method->__toString();
```

* Creating an interface method

```php
$method = (new PHPClassMethod(
    'write',
    [
        new PHPFunctionParameter('name', 'string'),
        new PHPFunctionParameter('params', PHPFunctionParameter::class)
    ],
))->throws([
    RuntimeException::class
])
->asCallableSignature()
->setReturnType(Stringable::class)
->setModifier(PHPTypesModifiers::PUBLIC);

// Method definition to string
$method->__toString();

```

* Creating PHP class

```php
use Drewlabs\CodeGenerator\Contracts\Stringable;
use Drewlabs\CodeGenerator\Models\PHPFunctionParameter;
use Drewlabs\CodeGenerator\Models\PHPClass;
use Drewlabs\CodeGenerator\Models\PHPClassMethod;
use Drewlabs\CodeGenerator\Models\PHPClassProperty;

    $class_ = (new PHPClass("Person", [], [
        (new PHPClassMethod('__construct', [
            new PHPFunctionParameter('firstname', 'string'),
            new PHPFunctionParameter('lastname', 'string')
        ], null, 'public', 'Class initializer')),
        (new PHPClassMethod('setFirstName', [
            new PHPFunctionParameter('firstname', 'string'),
            (new PHPFunctionParameter('default', 'string', 'DEFAULT'))->asOptional()
        ], "self", 'public', 'firstname property setter')),
        (new PHPClassMethod('setParent', [
            new PHPFunctionParameter('person',  "\\App\\Person\\Contracts\\PersonInterface")
        ], "self", 'public', 'parent property setter')),
        (new PHPClassMethod('getFirstName', [], "string", 'public', 'firstname property getter')),
    ],))->setBaseClass("\\App\\Core\\PersonBase")
        ->addImplementation("\\App\\Contracts\\PersonInterface")
        ->addImplementation("\\App\\Contracts\\HumanInterface")
        ->asFinal()
        ->addProperty(new PHPClassProperty('firstname', 'string', 'private', null, 'Person first name'))
        ->addConstant(new PHPClassProperty('lastname', 'string', 'private', null, 'Person last name'))
        ->addToNamespace("App\\Models");
// Convert class definition to string
$class_->__toString();
```

* Creating PHP Trait

```php
$trait_ = (new PHPTrait(
        "HasValidatableAttributes",
        [
            (new PHPClassMethod('setFirstName', [
                new PHPFunctionParameter('firstname', 'string')
            ], "self", 'public', 'firstname property setter')),
            (new PHPClassMethod('setParent', [
                new PHPFunctionParameter('person',  "\\App\\Person\\Contracts\\PersonInterface")
            ], "self", 'public', 'parent property setter')),
            (new PHPClassMethod('getFirstName', [], "string", 'public', 'firstname property getter')),
        ]
    ))
    ->addMethod((new PHPClassMethod('__construct', [
                new PHPFunctionParameter('firstname', 'string'),
                new PHPFunctionParameter('lastname', 'string')
            ], null, 'public', 'Class initializer')))
        ->addTrait('\\App\\Person\\Traits\\PersonInterface')
        ->addToNamespace("App\\Models")
        ->addProperty(new PHPClassProperty('firstname', 'string', 'private', null, 'Person first name'))
        ->addConstant(new PHPClassProperty('lastname', 'string', 'private', null, 'Person last name'));

// Convert trait definition to string
$trait_->__toString();
```

* Creating PHP interfaces

```php
$interface_ = (new PHPInterface(
        "Writer",
        [
            (new PHPClassMethod('setWriter', [
                new PHPFunctionParameter('writer', 'App\\Contracts\\Writer')
            ], "self", 'public', 'Writer property setter')),
            (new PHPClassMethod('write', [
                new PHPFunctionParameter('buffer', 'string')
            ], null, 'public', 'Write the buffer to the console')),
        ]
    ))->addToNamespace("App\\Contracts\\Writer")
        ->setBaseInterface('App\\Contracts\\Writer\\BufferWriter');

// Converting PHP interface definition to string
$interface_->__toString();
```
```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance43

Moderate activity, may be stable

Popularity11

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~41 days

Recently: every ~0 days

Total

18

Last Release

447d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/48c4973d500c7f4233d5ceacab51a57208d5fb60b0f95ae60264cf92380d0534?d=identicon)[azandrew-sidoine](/maintainers/azandrew-sidoine)

---

Top Contributors

[![azandrew-sidoine](https://avatars.githubusercontent.com/u/23530515?v=4)](https://github.com/azandrew-sidoine "azandrew-sidoine (80 commits)")

### Embed Badge

![Health badge](/badges/drewlabs-code-generator/health.svg)

```
[![Health](https://phpackages.com/badges/drewlabs-code-generator/health.svg)](https://phpackages.com/packages/drewlabs-code-generator)
```

###  Alternatives

[kbs1/laravel-abbreviations

Abbreviations / acronyms support for your app.

1018.6k](/packages/kbs1-laravel-abbreviations)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
