PHP code example of fratily / attribute-loader

1. Go to this page and download the library: Download fratily/attribute-loader 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/ */

    

fratily / attribute-loader example snippets


#[Attribute]
class FooAttribute {}

#[FooAttribute]
function target_function(): void {}

$loader = new Fratily\AttributeLoader\AttributeLoader(FooAttribute::class);
$attributes = $loader->load(new ReflectionFunction('target_function'));
var_dump($attributes[0]); // object(FooAttribute)

#[Attribute]
class FooAttribute {}

#[FooAttribute(name: 'abc')]
function target_function(int $number): void {}

$loader = new Fratily\AttributeLoader\AttributeLoader(
    FooAttribute::class,
    function (ReflectionAttribute $attr) {
        var_dump($attr->getArguments()); // array('name' => 'abc')

        // do something ...
        // ex: trigger event / customize attribute arguments ...

        // MUST return an instance of $attr->getName().
        // MUST not return a subclass of $attr->getName().
        return new FooAttribute();
    }
);
$attributes = $loader->load(new ReflectionFunction('target_function'));
var_dump($attributes[0]); // object(FooAttribute)