ReflectionProperty::getAttributes

Gets Attributes

&reftitle.description; public `array` `ReflectionProperty::getAttributes` ``string``null```name`&null; `int` `flags` 0

Returns all attributes declared on this class property as an array of `ReflectionAttribute`.

&reftitle.parameters;
&reflection.getattributes.param.name; &reflection.getattributes.param.flags;
&reftitle.returnvalues;

Array of attributes, as a `ReflectionAttribute` object.

&reftitle.examples;

### Basic usage

    <?php
    #[Attribute]
    class Fruit {
    }

    #[Attribute]
    class Red {
    }

    class Basket {
        #[Fruit]
        #[Red]
        public string $apple = 'apple';
    }

    $property = new ReflectionProperty('Basket', 'apple');
    $attributes = $property->getAttributes();
    print_r(array_map(fn($attribute) => $attribute->getName(), $attributes));
    ?>

&example.outputs;

    Array
    (
        [0] => Fruit
        [1] => Red
    )

### Filtering results by class name

    <?php
    #[Attribute]
    class Fruit {
    }

    #[Attribute]
    class Red {
    }

    class Basket {
        #[Fruit]
        #[Red]
        public string $apple = 'apple';
    }

    $property = new ReflectionProperty('Basket', 'apple');
    $attributes = $property->getAttributes('Fruit');
    print_r(array_map(fn($attribute) => $attribute->getName(), $attributes));
    ?>

&example.outputs;

    Array
    (
        [0] => Fruit
    )

### Filtering results by class name, with inheritance

    <?php
    interface Color {
    }

    #[Attribute]
    class Fruit {
    }

    #[Attribute]
    class Red implements Color {
    }

    class Basket {
        #[Fruit]
        #[Red]
        public string $apple = 'apple';
    }

    $property = new ReflectionProperty('Basket', 'apple');
    $attributes = $property->getAttributes('Color', ReflectionAttribute::IS_INSTANCEOF);
    print_r(array_map(fn($attribute) => $attribute->getName(), $attributes));
    ?>

&example.outputs;

    Array
    (
        [0] => Red
    )

&reftitle.seealso;

- `ReflectionClass::getAttributes`
- `ReflectionClassConstant::getAttributes`
- `ReflectionFunctionAbstract::getAttributes`
- `ReflectionParameter::getAttributes`