Opinion needed for constructor promotion #6606

Open
opened 2026-01-22 15:35:39 +01:00 by admin · 0 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?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6606