Projectsphp-builderInterfaces

PHP Builder

Package

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

Interfaces

PhpInterface emits a full interface file. Methods are forced to signature-only form (no bodies, no abstract keyword) regardless of how you configure the PhpMethod instances.

use BradieTilley\Builder\PhpArgument;
use BradieTilley\Builder\PhpInterface;
use BradieTilley\Builder\PhpMethod;
use BradieTilley\Builder\PhpProperty;
use BradieTilley\Builder\PhpPropertyGetHook;
use BradieTilley\Builder\Types\PhpUnionType;

$interface = new PhpInterface(
    namespace: 'App\\Contracts',
    name: 'Identifiable',
    extends: [
        'Stringable',
    ],
    properties: [
        new PhpProperty(
            type: 'string',
            name: 'id',
            get: new PhpPropertyGetHook(stub: true),
        ),
    ],
    methods: [
        new PhpMethod(
            name: 'resolve',
            args: [
                new PhpArgument(
                    type: new PhpUnionType(['string', 'int']),
                    name: 'key',
                ),
            ],
            return: 'static',
            description: 'Resolve by key',
            // lines are ignored for interfacessignature only
            lines: ['return $this;'],
        ),
    ],
    description: 'Something with an identity',
);

echo $interface->toPhp();

Constructor options

ArgumentTypeDefaultNotes
namestringShort interface name
namespacestring''
extendslist<string>|string[]Parent interfaces
constantslist<PhpClassConstant>[]
propertieslist<PhpProperty>[]Typically hook stubs for interface properties
methodslist<PhpMethod>[]Bodies stripped; rendered as signatures
attributeslist<PhpAttribute>[]
description?stringnull
templateslist<PhpTemplate|string>[]
strictTypesbooltrue

Interface properties

PHP interfaces may declare properties with hook stubs. Use PhpPropertyGetHook(stub: true) / PhpPropertySetHook(stub: true):

use BradieTilley\Builder\PhpProperty;
use BradieTilley\Builder\PhpPropertyGetHook;
use BradieTilley\Builder\PhpPropertySetHook;

new PhpProperty(
    type: 'string',
    name: 'name',
    get: new PhpPropertyGetHook(stub: true),
    set: new PhpPropertySetHook(stub: true),
);

Emits:

public string $name {
    get;
    set;
}

See Properties & Hooks for full hook options.

Method signatures

Any lines you set on interface methods are ignored in the emitted PHP. Return types, arguments, templates, @throws, attributes, and descriptions still render on the signature / docblock.

Continue to Traits.