PHPackages                             jmacedo/php-simple-annotation - 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. jmacedo/php-simple-annotation

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

jmacedo/php-simple-annotation
=============================

Yet another PHP annotation implementation. A simple and direct implementation without any complexity.

2.0.1(5y ago)16MITPHPPHP ^7.4

Since Aug 10Pushed 5y ago1 watchersCompare

[ Source](https://github.com/jmacedo/php-simple-annotation)[ Packagist](https://packagist.org/packages/jmacedo/php-simple-annotation)[ Docs](https://github.com/jmacedo/php-simple-annotation)[ RSS](/packages/jmacedo-php-simple-annotation/feed)WikiDiscussions master Synced 4d ago

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

PHP Simple Annotation
=====================

[](#php-simple-annotation)

[![Latest Stable Version](https://camo.githubusercontent.com/2318f0d68aefed6aab2f5edcae1209e1dd15ec31398783cd9ec7642a8e9ee2f6/68747470733a2f2f706f7365722e707567782e6f72672f6a6d616365646f2f7068702d73696d706c652d616e6e6f746174696f6e2f76)](//packagist.org/packages/jmacedo/php-simple-annotation) [![Total Downloads](https://camo.githubusercontent.com/8362bf2e54cf9e40f45f8b71e298873a59d808d6733116bc5eb379b0a698c3ad/68747470733a2f2f706f7365722e707567782e6f72672f6a6d616365646f2f7068702d73696d706c652d616e6e6f746174696f6e2f646f776e6c6f616473)](//packagist.org/packages/jmacedo/php-simple-annotation) [![Latest Unstable Version](https://camo.githubusercontent.com/9dfc7dfa594bed25e3069d8e7c4c6931b0c664ed5dd9ddc9107b0e0d6a6357f1/68747470733a2f2f706f7365722e707567782e6f72672f6a6d616365646f2f7068702d73696d706c652d616e6e6f746174696f6e2f762f756e737461626c65)](//packagist.org/packages/jmacedo/php-simple-annotation) [![License](https://camo.githubusercontent.com/604c6b964aac071948bc408fdb943a932aaa31b561e0a759018091250b829458/68747470733a2f2f706f7365722e707567782e6f72672f6a6d616365646f2f7068702d73696d706c652d616e6e6f746174696f6e2f6c6963656e7365)](//packagist.org/packages/jmacedo/php-simple-annotation) [![Build Status](https://camo.githubusercontent.com/48c88b05882fad6707f0662da9912c0c5a50dbf0a7ea8eb921191d1debc19bee/68747470733a2f2f7472617669732d63692e636f6d2f6a6d616365646f2f7068702d73696d706c652d616e6e6f746174696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/jmacedo/php-simple-annotation) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/fd534f75c251aa0242cbc251234e298bd171e1131cc38b74ae8d0c6bb5ac5c82/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6d616365646f2f7068702d73696d706c652d616e6e6f746174696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jmacedo/php-simple-annotation/?branch=master) [![Code Coverage](https://camo.githubusercontent.com/b9a749e12244509b4e9a677a52683fb3b90c4ca0ca55f6ecba61eb8bba86d623/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6d616365646f2f7068702d73696d706c652d616e6e6f746174696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jmacedo/php-simple-annotation/?branch=master) [![Code Intelligence Status](https://camo.githubusercontent.com/fc4c76b14a707e713008d5f85279995e3ae10cd245c2cda21ae403d7232346fe/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6d616365646f2f7068702d73696d706c652d616e6e6f746174696f6e2f6261646765732f636f64652d696e74656c6c6967656e63652e7376673f623d6d6173746572)](https://scrutinizer-ci.com/code-intelligence)

Yet another PHP annotation implementation. A simple and direct implementation without any complexity.

Requirements
------------

[](#requirements)

- PHP &gt;= 7.4.\*
- ext-json
- ext-mbstring

Installation
------------

[](#installation)

```
composer require jmacedo/php-simple-annotation
```

Usage
-----

[](#usage)

Consider the following class for the purpose of this documentation:

```
namespace Examples;

/**
 * Class MyAnnotatedClass
 *
 * @package Examples
 * @metadata {"PI": 3.14, "name": "The name", "genre": "The genre", "age": "The age"}
 * @PI 3.14
 * @list ['item 1', 'item 2', 'item 3']
 * @uselessClass true
 */
class MyAnnotatedClass
{
    /**
     * @value 3.14
     * @var double
     * @description Math constant
     */
    const PI = 3.14;

    /** @var string */
    public $name;

    /** @var string */
    protected $genre;

    /** @var int */
    private $age;

    /**
     * MyAnnotatedClass constructor.
     *
     * @param string $name
     * @param string $genre
     * @param int $age
     */
    public function __construct(string $name = '', string $genre = '', int $age = 0)
    {
        $this->name = $name;
        $this->genre = $genre;
        $this->age = $age;
    }

    /**
     * A method that says "Hello!".
     *
     * @methodName sayHello
     * @emptyAnnotation
     */
    public function sayHello()
    {
        echo 'Hello!';
    }
}
```

You can instantiate the Annotation class in two ways:

- First:

```
use SimpleAnnotation\Annotation;
use Examples\MyAnnotatedClass;

$annotation = new Annotation(MyAnnotatedClass::class);
// Or
$annotation = new Annotation('Examples\MyAnnotatedClass');
```

- Second:

```
use SimpleAnnotation\Annotation;
use Examples\MyAnnotatedClass;

$myAnnotatedClass = new MyAnnotatedClass();
$annotation = new Annotation($myAnnotatedClass);
```

To get class annotations:

```
$classAnnotations = $annotation->getClassAnnotations();

var_dump($classAnnotations);
```

```
object(SimpleAnnotation\AnnotationParsed)[6]
  private 'properties' =>
    array (size=6)
      'package' => string 'Examples' (length=8)
      'metadata' =>
        object(stdClass)[5]
          public 'PI' => float 3.14
          public 'name' => string 'The name' (length=8)
          public 'genre' => string 'The genre' (length=9)
          public 'age' => string 'The age' (length=7)
      'PI' => float 3.14
      'list' =>
        array (size=3)
          0 => string 'item 1' (length=6)
          1 => string 'item 2' (length=6)
          2 => string 'item 3' (length=6)
      'uselessClass' => boolean true
      'anotherStringValue' => string 'my string' (length=9)

```

The `AnnotationParsed` class implements `__get` and `__set` magic methods, so to access the annotations keys, just access the properties:

```
echo "PI value is:" . $classAnnotations->PI;
echo "PI is also available in: " . $classAnnotations->metadata->PI;
```

To get methods annotations:

```
$methodsAnnotations = $annotation->getMethodsAnnotations();

var_dump($methodsAnnotations);
```

```
array (size=2)
  '__construct' =>
    object(SimpleAnnotation\AnnotationParsed)[9]
      private 'properties' =>
        array (size=1)
          'param' =>
            array (size=3)
              0 => string 'string $name' (length=12)
              1 => string 'string $genre' (length=13)
              2 => string 'int $age' (length=8)
  'sayHello' =>
    object(SimpleAnnotation\AnnotationParsed)[10]
      private 'properties' =>
        array (size=2)
          'methodName' => string 'sayHello' (length=8)
          'emptyAnnotation' => boolean true

```

For a specific method:

```
$sayHelloAnnotations = $annotation->getMethodAnnotations('sayHello');

var_dump($sayHelloAnnotations);
```

```
object(SimpleAnnotation\AnnotationParsed)[11]
  private 'properties' =>
    array (size=2)
      'methodName' => string 'sayHello' (length=8)
      'emptyAnnotation' => boolean true

```

To get properties annotations:

```
$propertiesAnnotations = $annotation->getPropertiesAnnotations();

var_dump($propertiesAnnotations);
```

```
array (size=3)
  'name' =>
    object(SimpleAnnotation\AnnotationParsed)[12]
      private 'properties' =>
        array (size=1)
          'var' => string 'string' (length=6)
  'genre' =>
    object(SimpleAnnotation\AnnotationParsed)[13]
      private 'properties' =>
        array (size=1)
          'var' => string 'string' (length=6)
  'age' =>
    object(SimpleAnnotation\AnnotationParsed)[14]
      private 'properties' =>
        array (size=1)
          'var' => string 'int' (length=3)

```

For a specific property:

```
$nameAnnotations = $annotation->getPropertyAnnotations('name');

var_dump($nameAnnotations);
```

```
object(SimpleAnnotation\AnnotationParsed)[7]
  private 'properties' =>
    array (size=1)
      'var' => string 'string' (length=6)

```

And that's it
-------------

[](#and-thats-it)

Just instantiate the `SimpleAnnotation\Annotation` class, call the methods and use the annotations values of the class you want to.

Cache
-----

[](#cache)

By default, there is no cache in use, but there is a cache handler implemented that can be used, if you want.

This cache handler uses a file approach and had good results in some simple benchmarks I have made:

```
$annotation = new Annotation(MyAnnotatedClass::class);
$start = microtime(true);
for ($i = 0; $i < $numberOfTimes; $i++) {
    $classAnnotation = $annotation->getClassAnnotations();
    $methodsAnnotations = $annotation->getMethodsAnnotations();
    $propertiesAnnotations = $annotation->getPropertiesAnnotations();
}
$end = microtime(true);
echo 'Time elapsed without cache: ' . ($end - $start) . "\n";

$cache = new SimpleAnnotation\Concerns\Cache\FileCache(__DIR__ . DIRECTORY_SEPARATOR . 'test.txt');
$annotation2 = new Annotation(MyAnnotatedClass::class, $cache);
$start2 = microtime(true);
for ($i = 0; $i < $numberOfTimes; $i++) {
    $classAnnotation = $annotation2->getClassAnnotations();
    $methodsAnnotations = $annotation2->getMethodsAnnotations();
    $propertiesAnnotations = $annotation2->getPropertiesAnnotations();
}
$end2 = microtime(true);
echo 'Time elapsed with cache: ' . ($end2 - $start2) . "\n";
```

For `$numberOfTimes=100`:

```
Time elapsed without cache: 0.011738777160645
Time elapsed with cache: 0.00053501129150391
```

For `$numberOfTimes=10000`:

```
Time elapsed without cache: 1.1012320518494
Time elapsed with cache: 0.055597066879272
```

For `$numberOfTimes=100000`:

```
Time elapsed without cache: 11.216305971146
Time elapsed with cache: 0.53948402404785
```

The concrete implementation of `\SimpleAnnotation\Concerns\ParsedAnnotation` must implement the interface `\JsonSerializable` to the `SimpleAnnotation\Concerns\Cache\FileCache` implementation work well.

If you use the default implementation of this library, you don't have to worry about it.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity57

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 ~4 days

Total

3

Last Release

2097d ago

Major Versions

1.0.0 → 2.0.02020-08-11

### Community

Maintainers

![](https://www.gravatar.com/avatar/f05581256a934f9711bc5ab12cf8fb530affdfe782d5b5d359b1f3ece83922de?d=identicon)[jose.macedo](/maintainers/jose.macedo)

---

Top Contributors

[![jmacedo](https://avatars.githubusercontent.com/u/618950?v=4)](https://github.com/jmacedo "jmacedo (25 commits)")

---

Tags

annotation-parserannotation-processorannotationsphp-annotationannotation

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jmacedo-php-simple-annotation/health.svg)

```
[![Health](https://phpackages.com/badges/jmacedo-php-simple-annotation/health.svg)](https://phpackages.com/packages/jmacedo-php-simple-annotation)
```

###  Alternatives

[koriym/attributes

An annotation/attribute reader

433.4M12](/packages/koriym-attributes)[hyperf/di

A DI for Hyperf.

182.8M594](/packages/hyperf-di)[gomachan46/state-machine

simple state machine with annotations for PHP, inspired by AASM known as a Ruby state machine.

1893.9k](/packages/gomachan46-state-machine)[marcelog/ding

PHP Dependency Injection based on Spring(tm), with Aspect Oriented Programming, MVC

1202.1k](/packages/marcelog-ding)[marcin-orlowski/lombok-php

Never write boilerplate code for your data class again!

3118.6k1](/packages/marcin-orlowski-lombok-php)[antares/accessible

PHP library that allows you to define your class' getters, setters and constructor with docblock annotations.

123.9k1](/packages/antares-accessible)

PHPackages © 2026

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