PHP code example of atomino / util-attr

1. Go to this page and download the library: Download atomino/util-attr 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/ */

    

atomino / util-attr example snippets


get(\ReflectionClass|\ReflectionMethod|string $reflection, string|null $method = null):static|null
all(\ReflectionClass|\ReflectionMethod|string $reflection, string|null $method = null):static|null
collect(\ReflectionClass|\ReflectionMethod ...$reflections):static[]

#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
class MyAttr extends \Atomino\Neutrons\Attr
{
  public function __construct(public string $name){...}
}

#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
class MyRepeatableAttr extends \Atomino\Neutrons\Attr
{
  public function __construct(public string $name){...}
}

#[MyAttr("my awesome class")]
#[MyRepeatableAttr("my awesome class")]
class MyClass
{
  #[MyAttr("my awesome method")]
  #[MyRepeatableAttr("my awesome method")]
  #[MyRepeatableAttr("my awesome method two")]
  public function myMethod(){...}
}

get(\ReflectionClass|\ReflectionMethod|string $reflection, string|null $method = null):static|null

$classRef = new ReflectionClass(MyClass::class);
$attr = MyAttr::get($classRef);
echo $attr->name;

$methodRef = $classRef->getMethod("myMethod");
$attr = MyAttr::get($methodRef);
echo $attr->name;

$attr = MyAttr::get(MyClass::class);
echo $attr->name;

$attr = MyAttr::get(MyClass::class, "myMethod");
echo $attr->name;

all(\ReflectionClass|\ReflectionMethod|string $reflection, string|null $method = null):static|null

$classRef = new ReflectionClass(MyClass::class);
$attrs = MyRepeatableAttr::all($classRef);
foreach ($attrs as $attr) echo $attr->name;

$methodRef = $classRef->getMethod("myMethod");
$attrs = MyRepeatableAttr::all($methodRef);
foreach ($attrs as $attr) echo $attr->name;

$attrss = MyRepeatableAttr::all(MyClass::class);
foreach ($attrs as $attr) echo $attr->name;

$attrs = MyRepeatableAttr::all(MyClass::class, "myMethod");
foreach ($attrs as $attr) echo $attr->name;

collect(\ReflectionClass|\ReflectionMethod ...$reflections):static[]

$classRef = new ReflectionClass(MyClass::class);
$methodRefs = $classRef->getMethods();
$attrs = MyAttr::collect($classRef, ...$methodRefs);
foreach ($attrs as $attr) echo $attr->name;