PHP code example of pitch / annotation

1. Go to this page and download the library: Download pitch/annotation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pitch / annotation example snippets


namespace App;

use Attribute;
use Doctrine\Common\Annotation\Reader as DoctrineReader;
use Pitch\Annotation\Annotation;
use Pitch\Annotation\Reader as PitchReader;

/**
 * @Annotation
 */
#[Attribute]
class MyAnnotation implements Annotation
{
    public string $value;
}

/**
 * @MyAnnotation('foo')
 */
class MyClass
{
    #[MyAnnotation('bar')]
    public function myMethod() {}
}

$pitchReader = new PitchReader(new DoctrineReader());
$reflection = new ReflectionMethod(MyClass::class, 'myMethod');

foreach($pitchReader->getAnnotations($reflection)->all() as $annotation) {
    echo $annotation->value; // outputs: foobar
}

namespace App\Annotation;

#[Attribute]
class MyAnnotation
{
    public function __construct(
        public string $value,
    ) {}
}

namespace App\Controller;

use Symfony\Component\HttpFoundation\Request;
use App\Annotation\MyAnnotation;

class MyController {
    #[MyAnnotation("foo")]
    #[MyAnnotation("bar")]
    public function __invoke(Request $request)
    {
        foreach ($request->attributes->get('_' . MyAnnotation::class) as $a) {
            echo $a->value; // outputs: foobar
        }
    }
}