phpnomad / decorator
Assists with implementing decorator patterns.
Requires (Dev)
- phpnomad/tests: ^0.1.0
This package is auto-updated.
Last update: 2026-04-15 15:44:55 UTC
README
phpnomad/decorator is a small helper for writing decorator classes in PHP. It ships a single trait, WithDecoratedInstance, that forwards method calls to a wrapped object through __call() using an explicit allowlist, so a decorator only has to implement the behavior it actually changes.
Installation
composer require phpnomad/decorator
Quick Start
Add the trait to a class that wraps another instance, assign the wrapped object, and list the methods you want forwarded.
<?php use PHPNomad\Decorator\Traits\WithDecoratedInstance; class CachingRepository { use WithDecoratedInstance; protected array $allowedMethods = ['find', 'all']; public function __construct(Repository $repository) { $this->decoratedInstance = $repository; } public function save(Model $model): void { // Custom behavior here, then delegate if needed. $this->decoratedInstance->save($model); } }
Calls to find() or all() on a CachingRepository are forwarded to the wrapped Repository. Calls to any method that is not on $allowedMethods and is not defined on the decorator itself raise an E_USER_ERROR.
Overview
The package exports one trait, PHPNomad\Decorator\Traits\WithDecoratedInstance. Mixing it into a class adds four things.
- A
$decoratedInstanceproperty that holds the object being wrapped. - An
$allowedMethodsarray so that method forwarding is opt-in rather than automatic. - A
__call()implementation that routes allowlisted method calls to$decoratedInstanceviacall_user_func_array. - An explicit
E_USER_ERRORfor any other inaccessible method call, so typos and unintended forwards fail loudly instead of silently.
Because the allowlist is a plain array property, you control exactly which parts of the wrapped object's surface leak through the decorator.
Documentation
Broader PHPNomad framework documentation lives at phpnomad.com.
License
Released under the MIT License.