DEV Community

Cover image for PHP Tips | Melhorando o seu código com Attributes🚀
Raziel Rodrigues
Raziel Rodrigues

Posted on • Edited on

1 1

PHP Tips | Melhorando o seu código com Attributes🚀

O assunto de hoje é Attributes. Neste artigo, vou explicar o que fiz e fornecer o link para o repositório com todo o conteúdo.

Attributes

Attributes nos dãoa possibilidade de trabalhar com meta dados isso siginifica que conseguimos passar informações e depois ter acesso a elas, via a API de reflexão

Vamos ao Código

Preparei um exemplo muito simples, que basicamente simula uma pessoa que está trabalhando, representando oito horas de trabalho via Fibers.

echo '<h1> Attributes: </h1> <hr>';

#[Attribute]
class Route
{
    public function __construct($nome)
    {
    }
}

#[Route('/home')]
class HomeController
{
}

$reflector = new \ReflectionClass(HomeController::class);
$attrs = $reflector->getAttributes();

foreach ($attrs as $attriubute) {
    var_dump($attriubute->getName());
    var_dump($attriubute->getArguments());
    var_dump($attriubute->getTarget());
    var_dump($attriubute->newInstance());
}

Enter fullscreen mode Exit fullscreen mode

Assim podemos acessa informações de uma classe em outra de forma simples e descomplicada.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs