Generate builder patterns for PHP classes using attributes.
- 🚀 Attribute-based: Use PHP attributes to mark classes for builder generation
- 🏃 Zero Runtime Overhead: Builders generated at build time, not runtime
- 📝 IDE Friendly: Full autocomplete and type checking support
- 🔧 Highly Configurable: Customize every aspect of generation
- 🎯 Type Safe: Preserves all type information from original classes
- 🏗️ Constructor Aware: Intelligently handles constructor parameters
composer require maxbeckers/php-builder-generator
Add to your composer.json
:
{
"config": {
"allow-plugins": {
"maxbeckers/php-builder-generator": true
}
}
}
{
"autoload": {
"psr-4": {
"App\\": ["src/", "generated/php-builder-generator/App/"]
}
}
}
Important: The path must include the namespace (e.g. .../App/), not just the base output directory.
After updating, run:
composer dump-autoload
<?php
namespace App\Model;
use MaxBeckers\PhpBuilderGenerator\Attributes\Builder;
#[Builder]
class User
{
public function __construct(
public string $name,
public string $email,
public ?int $age = null,
public array $roles = []
) {}
}
Builders are automatically generated during composer install/update
, or run:
./vendor/bin/php-builder-generator
Use your generated builder:
$user = UserBuilder::builder()
->name('John Doe')
->email('[email protected]')
->age(30)
->roles(['admin'])
->build();
- Installation & Setup - Detailed installation guide
- Basic Usage - Learn the fundamentals
- Configuration Guide - All configuration options
- Basic Examples - Common use cases
- Contributing - How to contribute
- PHP 8.2 or higher
- Composer 2.0 or higher
If you find this package helpful, I would be happy to get a ⭐ star on GitHub! It helps others discover the project and motivates continued development.
This project is licensed under the MIT License - see the LICENSE file for details.
Questions or Issues? Please open an issue on GitHub.
Built with ❤️ for PHP developers