Projectsphp-builderProperties

PHP Builder

Package

A PHP code builder for programmatically generating classes, interfaces, traits, and enums.

Properties & Hooks

PhpProperty models class/trait/interface properties, including asymmetric visibility and PHP property hooks.

use BradieTilley\Builder\PhpAttribute;
use BradieTilley\Builder\PhpProperty;
use BradieTilley\Builder\Types\PhpUnionType;

new PhpProperty(
    type: 'string',
    name: 'title',
    description: 'Display title',
    defaultValue: "''",
    attributes: [
        new PhpAttribute('App\\Attributes\\Column', ['length: 255']),
    ],
);

new PhpProperty(
    type: new PhpUnionType(['string', 'int']),
    name: 'code',
    visibility: PhpProperty::protected(),
    setVisibility: PhpProperty::private(),
);

Property options

ArgumentTypeDefaultNotes
namestringWithout $
typePhpType|string|nullnull
visibilityPhpVisibilityPublicGet visibility
setVisibility?PhpVisibilitynullWhen different → public private(set)
staticboolfalse
readonlyboolfalseIncompatible with hooks
abstractboolfalseRequires at least one hook; no default
finalboolfalseIncompatible with abstract
defaultValue?stringnullRaw PHP; omitted when hooks are present
description?stringnullDocblock summary
get?PhpPropertyGetHooknull
set?PhpPropertySetHooknull
attributeslist<PhpAttribute>[]

Types that need PHPDoc (e.g. array<string>) automatically add a @var tag.

Asymmetric visibility

new PhpProperty(
    type: 'string',
    name: 'code',
    visibility: PhpProperty::protected(),
    setVisibility: PhpProperty::private(),
);
protected private(set) string $code;

Property hooks

Hooks support three shapes: stub, expression, and block.

Get hooks (PhpPropertyGetHook)

ArgumentNotes
stubEmits get; (interfaces / abstract)
expressionEmits get => $expr;
linesEmits a { … } body
byRefEmits &get
use BradieTilley\Builder\PhpPropertyGetHook;

// Stub
new PhpPropertyGetHook(stub: true);

// Expression
new PhpPropertyGetHook(byRef: true, expression: '$this->slug');

// Block
new PhpPropertyGetHook(lines: ['return strtoupper($this->title);']);

Set hooks (PhpPropertySetHook)

ArgumentNotes
stubEmits set; — no type/expression/lines allowed
typeRequired unless stub
nameParameter name (default value)
expressionEmits set(Type $name) => $expr;
linesEmits a block body
use BradieTilley\Builder\PhpPropertySetHook;

new PhpPropertySetHook(
    type: 'string',
    expression: '$this->slug = strtolower($value)',
);

new PhpPropertySetHook(
    type: 'string',
    name: 'incoming',
    lines: ['$this->title = $incoming;'],
);

Full example

new PhpProperty(
    type: 'string',
    name: 'label',
    final: true,
    get: new PhpPropertyGetHook(lines: ['return strtoupper($this->title);']),
    set: new PhpPropertySetHook(
        type: 'string',
        name: 'incoming',
        lines: ['$this->title = $incoming;'],
    ),
);
final public string $label {
    get {
        return strtoupper($this->title);
    }
    set(string $incoming) {
        $this->title = $incoming;
    }
}

Validation rules

The builder throws InvalidPhpDefinitionException when:

  • Hooks are combined with readonly
  • A property is both abstract and final
  • An abstract property has no hooks, or has a default value
  • A stub hook also has an expression or body lines
  • An expression hook also has body lines
  • A non-stub set hook omits its parameter type

Continue to Types.