PHP code example of authanram / attributes

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

    

authanram / attributes example snippets




namespace AwesomeProject\Attributes;

#[Attribute(Attribute::TARGET_PROPERTY)]
AwesomeAttribute
{
    /**
     * @param mixed ...$args // ['whatever', 'data']
     */
    public funtion __construct(mixed ...$args)
    {}
    
    /**
     * @param object $context   // Instance of AwesomeProject\AwesomeClass
     * @param string $name      // 'foo' (The name of the property the attribute belongs to)
     * @param mixed ...$args    // ['your', 'args']
     * 
     * @return void
     */
    public function handle(object $context, string $name, mixed ...$args)
    {
    }
    
    /**
     * @param mixed $old // 'qux'
     * 
     * @return string
     */
    public function value(mixed $old): string
    {
        return 'bar';
    }
}



namespace AwesomeProject;

use Authanram\Attributes;

AwesomeClass
{
    #[Attributes\AwesomeAttribute('whatever', 'data')]
    public string $foo = 'qux';
    
    public function __construct()
    {
        new Attributes($this, 'your', 'args');
    }
}

$instance = new \AwesomeProject\AwesomeClass();

$instance->foo; // 'bar'