php-builder-generator

Quick Start Guide

Get up and running with PHP Builder Generator in just a few minutes!

Installation

  1. Install
    composer require maxbeckers/php-builder-generator
    
  2. Configure Composer to allow the plugin:
    {
      "config": {
        "allow-plugins": {
          "maxbeckers/php-builder-generator": true
        }
      }
    }
    
  3. Add autoload path:
    {
      "autoload": {
        "psr-4": {
          "App\\": ["src/", "generated/php-builder-generator/App/"]
        }
      }
    }
    

Your First Builder

1. Create a Class

<?php
// src/Model/User.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 = [],
        public bool $active = true
    ) {}
}

2. Generate the Builder

Run the generation command:

./vendor/bin/php-builder-generator

Or builders are automatically generated during composer install/update!

3. Use Your Builder

<?php

use App\Model\User;
use App\Model\UserBuilder; // Auto-generated!

$user = UserBuilder::builder()
    ->name('John Doe')
    ->email('john@example.com')
    ->age(30)
    ->roles(['admin', 'user'])
    ->active(true)
    ->build();

echo $user->name; // "John Doe"

That’s It! πŸŽ‰

You now have a fully functional builder for your User class with:

Next Steps

Common Patterns

Simple Data Class

#[Builder]
class Product
{
    public string $name;
    public float $price;
    public ?string $description = null;
}

With Validation

#[Builder]
class Email
{
    public function __construct(public string $address)
    {
        if (!filter_var($address, FILTER_VALIDATE_EMAIL)) {
            throw new InvalidArgumentException('Invalid email address');
        }
    }
}

Ready to dive deeper? Check out our configuration guide!