PHPackages                             dentelis/php7-attribute-reader - 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. dentelis/php7-attribute-reader

ActiveLibrary

dentelis/php7-attribute-reader
==============================

Library for reading php8 attributes from legacy php7 code

00PHP

Since Mar 10Pushed 2mo agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

PHP7 Attribute Reader
=====================

[](#php7-attribute-reader)

A fast and simple library that brings PHP 8 attribute syntax to PHP 7.2+. Read and parse PHP 8 style attributes from comments in legacy PHP codebases.

[![PHP Version](https://camo.githubusercontent.com/cd59ba2b0fddd0cb8a2521a4879d8e1f669d27280c478976a54f08dc3bc59db1/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344372e322d626c75652e737667)](https://php.net/)[![License](https://camo.githubusercontent.com/8bb50fd2278f18fc326bf71f6e88ca8f884f72f179d3e555e20ed30157190d0d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e2e737667)](LICENSE)

🎯 Why This Library?
-------------------

[](#-why-this-library)

PHP 8 introduced native attributes (also known as annotations in other languages), but many projects are still running on PHP 7.x. This library allows you to:

- **Write modern PHP 8 attribute syntax** that works in PHP 7.2+
- **Future-proof your codebase** - attributes written with this library work as comments in PHP 7 and as native attributes in PHP 8
- **Gradually migrate** from PHP 7 to PHP 8 without rewriting your metadata
- **Use attributes for frameworks** that don't support PHP 8 yet

📦 Installation
--------------

[](#-installation)

```
composer require dentelis/php7-attribute-reader
```

🚀 Quick Start
-------------

[](#-quick-start)

### Basic Usage with Plain Arrays

[](#basic-usage-with-plain-arrays)

```
use AttributeReader\AttributeReader;

class UserController
{
    #[Route('/api/users', 'GET')]
    #[Auth('admin')]
    public function getUsers()
    {
        // Your code here
    }
}

// Read attributes as plain arrays
$attributes = AttributeReader::getPlainAttributes(
    UserController::class,
    'getUsers'
);

// Result:
// [
//     ['name' => 'Route', 'arguments' => ['/api/users', 'GET']],
//     ['name' => 'Auth', 'arguments' => ['admin']]
// ]
```

### Advanced Usage with DTO Classes (Recommended)

[](#advanced-usage-with-dto-classes-recommended)

Define your attribute classes:

```
namespace App\Attributes;

class Route
{
    public $path;
    public $method;

    public function __construct(string $path, string $method = 'GET')
    {
        $this->path = $path;
        $this->method = $method;
    }
}

class Auth
{
    public $role;

    public function __construct(string $role)
    {
        $this->role = $role;
    }
}
```

Use them in your code:

```
class UserController
{
    #[Route('/api/users', 'POST')]
    #[Auth('admin')]
    public function createUser($data)
    {
        // Your code here
    }
}

// Create reader with attribute mapping
$reader = new AttributeReader([
    'Route' => \App\Attributes\Route::class,
    'Auth' => \App\Attributes\Auth::class,
]);

// Get attribute instances
$attributes = $reader->getAttributes(UserController::class, 'createUser');

// Work with strongly-typed objects
foreach ($attributes as $attribute) {
    if ($attribute instanceof \App\Attributes\Route) {
        echo "Path: {$attribute->path}, Method: {$attribute->method}\n";
        // Output: Path: /api/users, Method: POST
    }

    if ($attribute instanceof \App\Attributes\Auth) {
        echo "Required role: {$attribute->role}\n";
        // Output: Required role: admin
    }
}
```

🎨 Features
----------

[](#-features)

### Named Arguments Support

[](#named-arguments-support)

Use PHP 8 named arguments syntax:

```
class ProductController
{
    #[Route(path: '/products', method: 'GET')]
    #[Cache(ttl: 3600, enabled: true)]
    public function list()
    {
        // Your code here
    }
}

$reader = new AttributeReader([
    'Route' => \App\Attributes\Route::class,
    'Cache' => \App\Attributes\Cache::class,
]);

$attributes = $reader->getAttributes(ProductController::class, 'list');
```

### Mixed Positional and Named Arguments

[](#mixed-positional-and-named-arguments)

```
#[Route('/api/users', method: 'POST')]  // First arg positional, second named
#[Cache(ttl: 3600, maxSize: 100)]       // All named
#[Auth('admin')]                        // Positional
```

### Multiple Attributes

[](#multiple-attributes)

You can use multiple attributes per method:

```
// Separate lines
#[Route('/admin')]
#[Auth('admin')]
#[RateLimit(100)]
public function adminDashboard() {}

// Or comma-separated in one line
#[Route('/admin'), Auth('admin'), RateLimit(100)]
public function adminDashboard() {}
```

### Unmapped Attribute Handling

[](#unmapped-attribute-handling)

Control what happens when an attribute isn't mapped to a class:

```
// Ignore unmapped attributes (default)
$reader = new AttributeReader($map, AttributeReader::UNMAPPED_IGNORE);

// Throw exception for unmapped attributes
$reader = new AttributeReader($map, AttributeReader::UNMAPPED_THROW);
```

📖 Complete Example: Building a Router
-------------------------------------

[](#-complete-example-building-a-router)

Here's a practical example of building a simple router using attributes:

```
