PHPackages                             c01l/phpdecorator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. c01l/phpdecorator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

c01l/phpdecorator
=================

The phpdecorator library can be used to wrap functions of objects and classes with additional functionality. This is a feature that can be compared to Python decorators.

1.2.0(4y ago)1131MITPHPPHP ^8.0

Since Dec 29Pushed 4y ago1 watchersCompare

[ Source](https://github.com/c01l/phpdecorator)[ Packagist](https://packagist.org/packages/c01l/phpdecorator)[ RSS](/packages/c01l-phpdecorator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (4)Used By (1)

phpdecorator
============

[](#phpdecorator)

The phpdecorator library can be used to wrap functions of objects and classes with additional functionality. This is a feature that can be compared to Python decorators.

How to create your own decorators?
----------------------------------

[](#how-to-create-your-own-decorators)

1. Declare an attribute that can be put on methods extending the Decorator base-class provided by the library.
2. Implement the wrap function returning a function that will be called instead of the original function.
3. Use `call_user_func_array` together with `func_get_args` to call the original function.

```
#[\Attribute(\Attribute::TARGET_METHOD)]
class LoggingDecorator extends \C01l\PhpDecorator\Decorator
{
    public function wrap(callable $func): callable
    {
        return function () use ($func) {
            /** @var Logger $logger */
            $logger = $this->getContainer()->get(Logger::class);
            $logger->log("Started");
            $ret = call_user_func_array($func, func_get_args());
            $logger->log("Ended");
            return $ret;
        };
    }
}
```

Using decorators
----------------

[](#using-decorators)

Just annotate the relevant function on a class.

```
class SomeClass
{
    #[LoggingDecorator]
    public function foo(int $bar): int
    {
        return $bar;
    }
}
```

The functionality will only be replaced if the object is passed through the library:

```
$decoratorManager = new DecoratorManager();

$obj = $decoratorManager->instantiate(SomeClass::class); // only possible for classes with a parameter-less constructor!
// OR
$obj = new SomeClass();
$obj = $decoratorManager->decorate($obj); // creates a proxy object (do not use the original reference of the object!)
```

Advanced Usage
--------------

[](#advanced-usage)

### Passing a container for dependencies to the decorators

[](#passing-a-container-for-dependencies-to-the-decorators)

You can supply a container to the `DecoratorManager` which will passed on to each `Decorator` that will be instantiated.

```
$container = /* use some PSR-11 container */
$decoratorManager = new DecoratorManager(container: $container)
```

In the decorator you can fetch the container with `$this->getContainer()`.

### Caching the generated classes

[](#caching-the-generated-classes)

If you are using this library on a large amount of classes, it might be suitable to use the class cache. Then classes are generated once and can be optimized by the runtime.

Just supply a path to a folder where your runtime is allowed to read and write files.

```
$decoratorManager = new DecoratorManager(classCachePath: "/path/to/cache/");
```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 53.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

1599d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cf26b256852b55606907f4cd1d697996d0a9027baa3eb165dd0d19250f23429c?d=identicon)[c01l](/maintainers/c01l)

---

Top Contributors

[![c01l](https://avatars.githubusercontent.com/u/17108962?v=4)](https://github.com/c01l "c01l (8 commits)")[![verbal521](https://avatars.githubusercontent.com/u/253633?v=4)](https://github.com/verbal521 "verbal521 (7 commits)")

---

Tags

attributesdecoratorphp8

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/c01l-phpdecorator/health.svg)

```
[![Health](https://phpackages.com/badges/c01l-phpdecorator/health.svg)](https://phpackages.com/packages/c01l-phpdecorator)
```

###  Alternatives

[symfony/dependency-injection

Allows you to standardize and centralize the way objects are constructed in your application

4.2k431.1M7.5k](/packages/symfony-dependency-injection)[illuminate/contracts

The Illuminate Contracts package.

704122.9M10.1k](/packages/illuminate-contracts)[illuminate/container

The Illuminate Container package.

31278.1M2.0k](/packages/illuminate-container)[ecotone/ecotone

Supporting you in building DDD, CQRS, Event Sourcing applications with ease.

558549.8k17](/packages/ecotone-ecotone)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

728272.9k20](/packages/civicrm-civicrm-core)[internal/dload

Downloads binaries.

98142.7k10](/packages/internal-dload)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
