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
10
votes
3 answers
8
votes
1 answer

How are PHP attributes different from doc block annotations?

Lately, the attributes RFC has passed the voting stage. How they are different from DocBlock annotations, and what benefits will they bring? Consider simple Doctrine entity, before: /** * @ORM\Entity */ class Entity { // … } After, using…
Mike Doe
  • 16,349
  • 11
  • 65
  • 88
4
votes
1 answer

PHPStan is not interpreting a Symfony EntityRepository as a generic

I'm having trouble making sense of this PHPStan error. PHPStan is saying I need to provide the class string of an EntityRepository of an object. I am providing the class string of a ServiceEntityRepository (which extends EntityRepository) of class…
amacrobert
  • 2,707
  • 2
  • 28
  • 37
3
votes
0 answers

Make modification to arguments before a method gets called

What would be the best way to perform some modification on a method argument before the method is actually executed? I have tried achieving this with a combination of attributes and use of the decorator/proxy pattern: #[\Attribute] class…
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
3
votes
1 answer

PHP 8 Attribute constructor calling

I'm trying to learn how attributes in PHP work. But when I wrote some code (I have XAMPP with PHP 8 support installed), it doesn't seem to work (no message on the screen). Does it need additonal configuration to work? use Attribute; class…
dominiq007
  • 45
  • 2
  • 4
2
votes
1 answer

How to create a preset of attributes using an attributes?

I'm using symfony 5.4 with php 8.1 and I would like to factor a set of attributes used for Swagger documentation, before each controllers method, I have, at least, these 5 attributes: #[OA\Response( response: 400, description: 'Bad…
Taine
  • 23
  • 3
2
votes
2 answers

How to replace phpdocs with new attributes in php 8

I am using laravel. I need to know how phpdoc can be written in php 8 with attibutes. /** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request * @return array */ public function…
2
votes
2 answers

Doctrine ORM 2.9 use both AnnotationDriver and AttributeDriver to parse entity metadata

Recently we upgraded our applications to PHP8. Since PHP8 introduced attributes and doctrine/orm supports them as of version 2.9 it seemed like a good idea to utilize this feature to incrementally (ie. not all entities at once) update entity…
helvete
  • 2,455
  • 13
  • 33
  • 37
1
vote
0 answers

How to use PHP Attributes as Events?

Lets say i have an attribute class: #[Attribute(Attribute::TARGET_METHOD)] class MyEvent { /** * @phpstan-param null|Request::METHOD_* $method */ public function __construct( public string $eventName, public ?string…
Stackster
  • 11
  • 1
1
vote
1 answer

How to get method name from PHP 8 attribute (reflection)

I'm working on small PHP framework (for learning). It has a file with LoginController class containing a method with Route attribute (code below). Is there any way to get the name of the method in the Route attribute class using Reflection? Class…
dominiq007
  • 45
  • 2
  • 4
1
vote
1 answer

PHP 8 Attributes: Is it possible to validate metadata passed to attributes or force attribute instantiation?

Playing a little bit with the new PHP 8 attributes (annotations) (https://stitcher.io/blog/attributes-in-php-8) I started to create my own ones. Simple example: namespace CisTools\Attribute; use Attribute; use…
Blackbam
  • 17,496
  • 26
  • 97
  • 150
0
votes
1 answer

PHP attribute get target

Let's say I have the following classes: class Bar{ public int $id; } class Boo{ public string $name; } class Foo{ #[ObjectAttr] public Bar $barObject; #[ObjectAttr] public Boo $booObject; } #[\Attribute] class…
Pejman
  • 2,442
  • 4
  • 34
  • 62
0
votes
1 answer

Method with a SerializedName attribute getting ignored when object is serialized with symfony serializer

I have a symfony serializer that i initialize this way : $this->serializer = new Serializer( [ new ArrayDenormalizer(), new UidNormalizer(), new BackedEnumNormalizer(), new DateTimeNormalizer(), new…
Methraen
  • 11
  • 3
0
votes
1 answer

PHP use ENUM in Attributes

Look at the following code:
112Legion
  • 1,129
  • 12
  • 25
0
votes
0 answers

Composer package to explicitly allow annotations with symfony's maker bundle?

I'm working with Bolt CMS on a project. It seems great so far, but it looks like the Bolt team has been a bit cautious about switching from annotation mappings to attribute mappings, as when I use make:entity, I get this message: [ERROR] Only…
1
2