PHPackages                             sagittaracc/php-python-decorator - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. sagittaracc/php-python-decorator

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

sagittaracc/php-python-decorator
================================

Python style decorator for PHP

v6.4.3(1y ago)1473[2 issues](https://github.com/sagittaracc/decorator/issues)MITPHP

Since Jan 13Pushed 1y ago1 watchersCompare

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

READMEChangelog (10)Dependencies (2)Versions (48)Used By (0)

PHP Python Decorator
====================

[](#php-python-decorator)

Python style decorators for PHP

Requirements
============

[](#requirements)

PHP 8.1 or higher

Install
=======

[](#install)

`composer require sagittaracc/php-python-decorator`

Example
=======

[](#example)

How long it takes to run a method. See the [`Timer`](https://github.com/sagittaracc/php-python-decorator/blob/main/tests/decorators/Timer.php) decorator

```
use Sagittaracc\PhpPythonDecorator\Decorator;

class Calc
{
    use Decorator;

    #[Timer]
    function sum($a, $b)
    {
        sleep(1);
        return $a + $b;
    }
}
```

This is how you can call it

```
$calc = new Calc();
echo call_decorator_func_array([$calc, 'sum'], [1, 2]); // Total execution: 1.00034234 ms; Result: 3
```

Or inline

```
$timerOnSum = (new Timer)->wrapper(fn($a, $b) => $calc->sum($a, $b));
echo $timerOnSum(1, 2); // Total execution: 1.00034234 ms; Result: 3
```

Generics
========

[](#generics)

```
use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\modules\generics\aliases\T;
use Sagittaracc\PhpPythonDecorator\modules\validation\core\validators\ArrayOf;

#[T]
class Box
{
    use Decorator;

    #[ArrayOf(T::class)]
    public $items;

    public function addItem(#[T] $item)
    {
        $this->items[] = $item;
    }
}

$box = new Box();
$box(Pen::class); // new Box();
call_decorator_func_array([$box, 'addItem'], [new Pencil]); // throws a GenericError
```

Validation
==========

[](#validation)

```
use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\tests\examples\Progress;
use Sagittaracc\PhpPythonDecorator\tests\validators\Length;
use Sagittaracc\PhpPythonDecorator\tests\validators\SerializeOf;
use Sagittaracc\PhpPythonDecorator\tests\validators\In;
use Sagittaracc\PhpPythonDecorator\tests\validators\LessThan;
use Sagittaracc\PhpPythonDecorator\tests\validators\UInt8;

class Progress
{
    use Decorator;

    #[UInt8]
    public $max;

    #[UInt8]
    #[LessThan('max')]
    public $pos;

    #[In('progress', 'finish', 'aborted')]
    public $status;

    #[Length(32)]
    public string $caption;
}

$progress = new Progress();

set_decorator_prop($progress, 'max', 255);  // max uint8 - 255
set_decorator_prop($progress, 'pos', 100);  // should be less than max
set_decorator_prop($progress, 'status', 'progress');  // status is one of possible cases (progress, finish or aborted)
set_decorator_prop($progress, 'caption', 'in progress ...');  // just a string (max length is 32)
```

Rpc
===

[](#rpc)

```
use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\modules\rpc\core\Rpc;

#[Rpc]
class Controller
{
    use Decorator;

    public function sum($a, $b)
    {
        return $a + $b;
    }
}
```

in `index.php`

```
$requestBody = file_get_contents('php://input');

$controller = new Controller();
$controller($requestBody);
```

in `terminal`

```
$ curl -d "{"id":1,"method":"sum","params":[1,4]}" http://localhost:4000
{"json-rpc":"2.0","id":1,"result":5}
```

Console
=======

[](#console)

```
use Sagittaracc\PhpPythonDecorator\Decorator;
use Sagittaracc\PhpPythonDecorator\modules\console\core\Console;

class Controller
{
    use Decorator;

    #[Console('hello')]
    function greetingPerson($name)
    {
        return "Hello, $name";
    }
}
```

in the command line it would be calling for example something like this:

`php index.php -c hello --name Yuriy`

then in `index.php` you should read the command and the parameters and after that call it like this:

```
(new Console('hello'))->setParameters(['name' => 'Yuriy'])->getMethod(Controller::class)->run();
```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance36

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~14 days

Recently: every ~53 days

Total

47

Last Release

545d ago

Major Versions

v1.14.1 → v2.0.02023-02-21

v2.0.1 → v3.0.02023-02-22

v3.0.2 → v4.0.02023-03-02

v4.3.3 → v5.0.02024-03-29

v5.0.0 → v6.0.02024-03-29

### Community

Maintainers

![](https://www.gravatar.com/avatar/36b24611c7e054895a0b29bfc0ee8df3bc1c703acac8e5a82a61821cf6ac5401?d=identicon)[sagittaracc](/maintainers/sagittaracc)

---

Top Contributors

[![sagittaracc](https://avatars.githubusercontent.com/u/60045511?v=4)](https://github.com/sagittaracc "sagittaracc (105 commits)")

---

Tags

declarative-programmingdecoratorsgenericgeneric-librarygeneric-typesgenericspatternsphpphp-attributephp-attributesphp-genericsphp-libraryphp8pythonrpcrpc-apirpc-serverrpc-servicevalidationvalidator

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/sagittaracc-php-python-decorator/health.svg)

```
[![Health](https://phpackages.com/badges/sagittaracc-php-python-decorator/health.svg)](https://phpackages.com/packages/sagittaracc-php-python-decorator)
```

###  Alternatives

[webmozart/assert

Assertions to validate method input/output with nice error messages.

7.6k894.0M1.2k](/packages/webmozart-assert)[bensampo/laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.

2.0k15.9M104](/packages/bensampo-laravel-enum)[swaggest/json-schema

High definition PHP structures with JSON-schema based validation

48612.5M73](/packages/swaggest-json-schema)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[ashallendesign/laravel-config-validator

A package for validating your Laravel app's config.

217905.3k5](/packages/ashallendesign-laravel-config-validator)[crazybooot/base64-validation

Laravel validators for base64 encoded files

1341.9M8](/packages/crazybooot-base64-validation)

PHPackages © 2026

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