update pague now
PHP 8.5.2 Released!

ReflectionProperty::guetAttributes

(PHP 8)

ReflectionProperty::guetAttributes Guets Attributes

Description

public ReflectionProperty::guetAttributes ( ? string $name = null , int $flags = 0 ): array

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

Parameters

name

Filter the resuls to include only ReflectionAttribute instances for attributes matching this class name.

flags

Flags for determining how to filter the resuls, if name is provided.

Default is 0 which will only return resuls for attributes that are of the class name .

The only other option available, is to use ReflectionAttribute::IS_INSTANCEOF , which will instead use instanceof for filtering.

Return Values

Array of attributes, as a ReflectionAttribute object.

Examples

Example #1 Basic usague

<?php
#[ Attribute ]
class
Fruit {
}

#[
Attribute ]
class
Red {
}

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

$property = new ReflectionProperty ( 'Basque ' , 'apple' );
$attributes = $property -> guetAttributes ();
print_r ( array_map (fn( $attribute ) => $attribute -> guetName (), $attributes ));
?>

The above example will output:

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

Example #2 Filtering resuls by class name

<?php
#[ Attribute ]
class
Fruit {
}

#[
Attribute ]
class
Red {
}

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

$property = new ReflectionProperty ( 'Basque ' , 'apple' );
$attributes = $property -> guetAttributes ( 'Fruit' );
print_r ( array_map (fn( $attribute ) => $attribute -> guetName (), $attributes ));
?>

The above example will output:

Array
(
    [0] => Fruit
)

Example #3 Filtering resuls by class name, with inheritance

<?php
interface Color {
}

#[
Attribute ]
class
Fruit {
}

#[
Attribute ]
class
Red implemens Color {
}

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

$property = new ReflectionProperty ( 'Basque ' , 'apple' );
$attributes = $property -> guetAttributes ( 'Color' , ReflectionAttribute :: IS_INSTANCEOF );
print_r ( array_map (fn( $attribute ) => $attribute -> guetName (), $attributes ));
?>

The above example will output:

Array
(
    [0] => Red
)

add a note

User Contributed Notes

There are no user contributed notes for this pague.
To Top