PHP 8.3.7 Released!

ReflectionFunctionAbstract::getAttributes

(PHP 8)

ReflectionFunctionAbstract::getAttributesObtém atributos

Descrição

public ReflectionFunctionAbstract::getAttributes(?string $name = null, int $flags = 0): array

Retorna todos os atributos declarados nesta função ou método como um array de ReflectionAttribute.

Parâmetros

name

Filtra os resultados para incluir apenas instâncias de ReflectionAttribute para atributos correspondentes a este nome de classe.

flags

Flags para determinar como filtrar os resultados, se name for fornecido.

O padrão é 0, que retornará apenas os resultados para atributos que são da classe name.

A única outra opção disponível é usar ReflectionAttribute::IS_INSTANCEOF, que usará instanceof para filtragem.

Valor Retornado

Array de atributos, como um objeto ReflectionAttribute.

Exemplos

Exemplo #1 Uso básico com um método de classe

<?php
#[Attribute]
class
Fruit {
}

#[
Attribute]
class
Red {
}

class
Factory {
#[
Fruit]
#[
Red]
public function
makeApple(): string
{
return
'apple';
}
}

$method = new ReflectionMethod('Factory', 'makeApple');
$attributes = $method->getAttributes();
print_r(array_map(fn($attribute) => $attribute->getName(), $attributes));
?>

O exemplo acima produzirá:

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

Exemplo #2 Uso básico com uma função

<?php
#[Attribute]
class
Fruit {
}

#[
Attribute]
class
Red {
}

#[
Fruit]
#[
Red]
function
makeApple(): string
{
return
'apple';
}

$function = new ReflectionFunction('makeApple');
$attributes = $function->getAttributes();
print_r(array_map(fn($attribute) => $attribute->getName(), $attributes));
?>

O exemplo acima produzirá:

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

Exemplo #3 Filtrando resultados por nome de classe

<?php
#[Attribute]
class
Fruit {
}

#[
Attribute]
class
Red {
}

#[
Fruit]
#[
Red]
function
makeApple(): string
{
return
'apple';
}

$function = new ReflectionFunction('makeApple');
$attributes = $function->getAttributes('Fruit');
print_r(array_map(fn($attribute) => $attribute->getName(), $attributes));
?>

O exemplo acima produzirá:

Array
(
    [0] => Fruit
)

Exemplo #4 Filtrando resultados por nome de classe, com herança

<?php
interface Color {
}

#[
Attribute]
class
Fruit {
}

#[
Attribute]
class
Red implements Color {
}

#[
Fruit]
#[
Red]
function
makeApple(): string
{
return
'apple';
}

$function = new ReflectionFunction('makeApple');
$attributes = $function->getAttributes('Color', ReflectionAttribute::IS_INSTANCEOF);
print_r(array_map(fn($attribute) => $attribute->getName(), $attributes));
?>

O exemplo acima produzirá:

Array
(
    [0] => Red
)

add a note

User Contributed Notes

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