PHP code example of cspray / annotated-target

1. Go to this page and download the library: Download cspray/annotated-target 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/ */

    

cspray / annotated-target example snippets


composer 

 declare(strict_types=1);

#[ClassAttr]
class Foo {

    #[PropAttr]
    private string $prop = 'foo';

    #[MethodAttr]
    public function getProp() : string {
        return $this->prop;
    }
    
}

 declare(strict_types=1);

use function Cspray\AnnotatedTarget\parseAttributes;

// parseAttributes returns a Generator, iterate over it to retrieve all Attributes found
foreach (parseAttributes(__DIR__ . '/src') as $annotatedTarget) {
    // $annotatedTarget is an instanceof AnnotatedTarget
    // This will be a ReflectionClass, ReflectionProperty, or ReflectionMethod depending on which iteration
    $target = $annotatedTarget->getTargetReflection();
    // This will be a ReflectionAttribute
    $attributeReflection = $annotatedTarget->getAttributeReflection();
    // This will be an instance of the Attribute returned from $this->getAttributeReflection()->newInstance()
    $attributeInstance = $annotatedTarget->getAttributeInstance();
    
    // All the methods above are shared
    $isShared = $annotatedTarget->getTargetReflection() === $annotatedTarget->getTargetReflection(); // true
}

 declare(stric_types=1)

use function Cspray\AnnotatedTarget\parseAttributes;

foreach (parseAttributes(__DIR__ . '/src', [MethodAttr::class]) as $target) {
    // Only targets for usages of MethodAttr will be