Questions tagged [php-attributes]

Attributes help to add meta-data to PHP functions, parameters, classes, class methods, constants, properties, closures, and even anonymous classes. This meta-data can be fetched programmatically, and provides a pragmatic approach to resolve the attributes in elsewhere in the code.

Attributes offer the ability to add structured, machine-readable metadata information on declarations in code: Classes, methods, functions, parameters, properties and class constants can be the target of an attribute. The metadata defined by attributes can then be inspected at runtime using the Reflection APIs. Attributes could therefore be thought of as a configuration language embedded directly into code.

With attributes the generic implementation of a feature and its concrete use in an application can be decoupled. In a way it is comparable to interfaces and their implementations. But where interfaces and implementations are about code, attributes are about annotating extra information and configuration. Interfaces can be implemented by classes, yet attributes can also be declared on methods, functions, parameters, properties and class constants. As such they are more flexible than interfaces.

17 questions
0
votes
1 answer

Custom validator for email doesn't throw exception when validating an invalid email address on a Spatie DTO object

I'm currently working on an integration with a third party API for an application, for which I'm using Spatie's data transfer object library. I'm currently looking to set up validation for some fields, and have run into an issue. I've written the…
Matthew Daly
  • 9,212
  • 2
  • 42
  • 83
0
votes
0 answers

Attributes for Symfony route

In Symfony controllers, route are defined as follow : class BlogController extends AbstractController { #[Route('/blog/{page}', name: 'blog_list', requirements: ['page' => '\d+'])] public function list(int $page): Response { //…
Mulot
  • 280
  • 2
  • 16
1
2