The Attribute class on PHP Attributes Reader library is a class that helps to access details of a PHP attribute which was fetched through the AttributesReader
PHP Class with Class attributes and arguments(values) on those attributes
<?php
declare(strict_types=1);
#[TestAttribute11(["key1" => "key1 value"])]
#[TestAttribute12(10.13)]
#[TestAttribute13(123)]
#[TestAttribute14()]
#[TestAttribute15(["value1", "value2"])]
class Abc
{
// rest of the codes
}
Initialize the Attributes Reader and retrieve an attribute
<?php
declare(strict_types=1);
use \AntonDPerera\PHPAttributesReader\AttributesReader;
$class = Abc::class;
$attributes_reader = AttributesReader($class);
//This will return instance of AntonDPerera\PHPAttributesReader\Attribute
$attribute = $attributes_reader->getClassAttribute("TestAttribute11");
$arguments = $attribute->getArguments();
echo $arguments[0]->getType(); // return the value of constant Argument::ARGUMENT_VALUE_TYPE_ASSOCIATIVE_ARRAY
var_dump($arguments[0]->getValue()); // return array("key1" => "key1 value")
Refer to Class Attributes , Method Attributes and Property Attributes guides to see how to get attributes from the AttributesReader and then you can get arguments from those attributes using the same functions.
Available functions in Argument class
public function getType(): int
Returns type of the Argument(type of the value as an integer) using one of the below constants which are defined on Argument class.
public const ARGUMENT_VALUE_TYPE_EMPTY = 0;
public const ARGUMENT_VALUE_TYPE_BOOLEAN = 1;
public const ARGUMENT_VALUE_TYPE_STRING = 2;
public const ARGUMENT_VALUE_TYPE_INT = 3;
public const ARGUMENT_VALUE_TYPE_FLOAT = 4;
public const ARGUMENT_VALUE_TYPE_SEQUENTIAL_ARRAY = 5;
public const ARGUMENT_VALUE_TYPE_ASSOCIATIVE_ARRAY = 6;
public const ARGUMENT_VALUE_TYPE_OBJECT = 7;
public const ARGUMENT_VALUE_TYPE_OTHER = 50;
public function getValue(): mixed
Returns the value of the Argument.
Support for different types of Arguments(Attribute values) is currently very limited and will be improved in future releases.
Refer Limitations documentation to get an idea about the current capabilities and to evaluate this library for your needs.