Opinion needed for constructor promotion #6608

Closed
opened 2026-01-22 15:35:41 +01:00 by admin · 2 comments
Owner

Originally created by @zmitic on GitHub (Jan 18, 2021).

Recommended usage is to use annotations but with PHP8, would XML make more sense?

Previously:

class User
{
    /** @ORM\Column(type="string") */
    private string $firstName;
    
    public function __construct(string $firstName)
    {
        $this->firstName = $firstName;
    }
}

vs

class User
{
    public function __construct(private string $firstName)
    {
    }
}

and some xml. It is pretty decent removal of boilerplate code and just for one property.

Notes

  • all my entities get their dependencies via constructor so no need for nullable properties
  • collections would require $this->children = new ArrayCollection() but it is very rare I use m2m, or bidirectional relation

Is there something I am missing?

Originally created by @zmitic on GitHub (Jan 18, 2021). Recommended usage is to use annotations but with PHP8, would XML make more sense? Previously: ```php class User { /** @ORM\Column(type="string") */ private string $firstName; public function __construct(string $firstName) { $this->firstName = $firstName; } } ``` vs ```php class User { public function __construct(private string $firstName) { } } ``` and some xml. It is pretty decent removal of boilerplate code and just for one property. #### Notes - all my entities get their dependencies via constructor so no need for nullable properties - collections would require ``$this->children = new ArrayCollection()`` but it is **very** rare I use m2m, or bidirectional relation Is there something I am missing?
admin closed this issue 2026-01-22 15:35:41 +01:00
Author
Owner

@dbrumann commented on GitHub (Jan 18, 2021):

As far as I can tell your problem is that properties feel redundant in PHP 8 and are only necessary for putting the annotations on there. This should be solvable in the future with Attributes (when they become available, see #8266):

public function __construct(
    #[ORM\Column(type: 'string')] private string $firstName,
    ...
) { }

// or alternatively:

public function __construct(
    #[ORM\Column(type: 'string')]
    private string $firstName,
    ...
) { }
@dbrumann commented on GitHub (Jan 18, 2021): As far as I can tell your problem is that properties feel redundant in PHP 8 and are only necessary for putting the annotations on there. This should be solvable in the future with Attributes (when they become available, see #8266): ```php public function __construct( #[ORM\Column(type: 'string')] private string $firstName, ... ) { } // or alternatively: public function __construct( #[ORM\Column(type: 'string')] private string $firstName, ... ) { } ```
Author
Owner

@beberlei commented on GitHub (Jan 18, 2021):

This is not an issue, but a discussion topic. Doctrine works with ctor promotion. Please write to the mailing list or discuss this in the Doctrine Slack.

@beberlei commented on GitHub (Jan 18, 2021): This is not an issue, but a discussion topic. Doctrine works with ctor promotion. Please write to the mailing list or discuss this in the Doctrine Slack.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6608