PHP Builder
A fluent, object-oriented PHP code builder for programmatically generating classes, interfaces, traits, and enums — complete with imports, attributes, typed members, property hooks, and PHPDoc.
use BradieTilley\Builder\PhpArgument;
use BradieTilley\Builder\PhpClass;
use BradieTilley\Builder\PhpMethod;
use BradieTilley\Builder\Types\PhpGeneric;
$class = new PhpClass(
namespace: 'App\\Models',
name: 'Post',
extends: 'App\\Models\\Model',
);
$class->methods[] = new PhpMethod(
name: 'setTags',
args: [
new PhpArgument(
type: PhpGeneric::array(value: 'string'),
name: 'tags',
defaultValue: '[]',
),
],
return: 'static',
lines: [
'$this->tags()->sync($tags);',
'return $this;',
],
);
echo $class->toPhp();
Documentation
- Introduction
- Installation
- Classes
- Interfaces
- Traits
- Enums
- Methods & Arguments
- Properties & Hooks
- Types
- Imports & Aliasing
- Attributes & Constants
- Formatting
- Compatibility
Requirements
- PHP 8.5
bradietilley/php-data(pulled in automatically)
Quick start
composer require bradietilley/php-builder
use BradieTilley\Builder\PhpClass;
use BradieTilley\Builder\PhpMethod;
$class = new PhpClass(
namespace: 'App\\Support',
name: 'Greeter',
methods: [
new PhpMethod(
name: 'hello',
return: 'string',
lines: ["return 'hello';"],
),
],
);
file_put_contents('Greeter.php', $class->toPhp());
See Installation and Classes for the full walkthrough.