How do I use attributes/annotations in PHP?

In PHP, attributes (also known as annotations) provide metadata to classes, methods, and properties. This can be useful for various aspects such as validation, serialization, and more, especially when using frameworks like Symfony or Laravel. Attributes are defined using the `#[Attribute]` syntax.

PHP attributes, PHP annotations, metadata, Symfony, Laravel
This example illustrates how to create and use attributes in PHP to enhance class properties with metadata.
<?php #[Attribute] class ExampleAttribute { public function __construct(private string $name) {} } class User { #[ExampleAttribute('username')] public string $name; public function __construct(string $name) { $this->name = $name; } } // Usage $user = new User("JohnDoe"); ?>

PHP attributes PHP annotations metadata Symfony Laravel