# [RFC] Constructor Property Promotion

- Thread: https://externals.io/message/109335
- Original archive: https://news-web.php.net/php.internals/109335
- Started by Nikita Popov on 2020-03-26T13:30:46+00:00
- Messages: 46
- RFC: Constructor Property Promotion (Implemented (PHP 8.0)) — https://externals.io/rfc/constructor_promotion.md
- Page 1 of 2: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/109335.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #109335 — Nikita Popov — 2020-03-26T13:30:46+00:00

URL: https://externals.io/message/109335#109335

Hi internals,

I would like to submit the following RFC for your consideration:
https://wiki.php.net/rfc/constructor_promotion

This is based on one off the suggestions made in
https://externals.io/message/109220, and some existing discussion on the
topic can be found in that thread.

The primary motivation for this feature is to close the currently rather
wide gap between the use of ad-hoc array structures, and the use of
well-defined and type-safe value objects. The latter is what we want people
to use, but the former is, unfortunately, what is *easy* to use.

Regards,
Nikita

---

## #109336 — Paul M. Jones — 2020-03-26T13:33:45+00:00

URL: https://externals.io/message/109335#109336 · In reply to: #109335

> 
> 
> Hi internals,
> 
> I would like to submit the following RFC for your consideration:
> https://wiki.php.net/rfc/constructor_promotion

Big fan. Thanks for this.


-- 
Paul M. Jones
pmjones@pmjones.io
http://paul-m-jones.com

Modernizing Legacy Applications in PHP
https://leanpub.com/mlaphp

Solving the N+1 Problem in PHP
https://leanpub.com/sn1php

---

## #109337 — Sebastian Bergmann — 2020-03-26T15:45:19+00:00

URL: https://externals.io/message/109335#109337 · In reply to: #109335

Am 26.03.2020 um 14:30 schrieb Nikita Popov:
> I would like to submit the following RFC for your consideration:
> https://wiki.php.net/rfc/constructor_promotion

Looks good to me! Thanks.

---

## #109338 — David Rodrigues — 2020-03-26T16:01:57+00:00

URL: https://externals.io/message/109335#109338 · In reply to: #109337

Hello!

With all respect, seems a bit confuses to me, without in fact offer a new
feature, just a shortcut. I hope it's me seeing it the wrong way.

What happen if I rename the constructor parameters on a child class
constructor?

Example (https://3v4l.org/VqQde):

class A {
    public function __construct(public int $x) { ... }
}

class B extends A {
    public function __construct(public int $y) { ...;
parent::__construct($y); }
}


Atenciosamente,
David Rodrigues


Em qui., 26 de mar. de 2020 às 12:45, Sebastian Bergmann <sebastian@php.net>
escreveu:

> Am 26.03.2020 um 14:30 schrieb Nikita Popov:
> > […]
>
> Looks good to me! Thanks.

---

## #109339 — Chase Peeler — 2020-03-26T16:20:04+00:00

URL: https://externals.io/message/109335#109339 · In reply to: #109336

>
>
> > […]
>
> Big fan. Thanks for this.
>
>
I like this too. One question (and apologies if I overlooked this in the
RFC), but is type-hinting required, or would the following be accepted:

class bar {
   public function __construct(public $foo){}
}


>
> --
> Paul M. Jones
> pmjones@pmjones.io
> http://paul-m-jones.com
>
> Modernizing Legacy Applications in PHP
> https://leanpub.com/mlaphp
>
> Solving the N+1 Problem in PHP
> https://leanpub.com/sn1php
>
> --
> 
> 
>
>

-- 
Chase Peeler
chasepeeler@gmail.com

---

## #109341 — Sara Golemon — 2020-03-26T16:39:11+00:00

URL: https://externals.io/message/109335#109341 · In reply to: #109335

> I would like to submit the following RFC for your consideration:
> https://wiki.php.net/rfc/constructor_promotion
>
> This is based on one off the suggestions made in
> https://externals.io/message/109220, and some existing discussion on the
> topic can be found in that thread.
>
> Similar to an RFC you submitted six years ago:
https://wiki.php.net/rfc/automatic_property_initialization
I think the availability of typed properties makes the syntax for this much
more sensible and I look forward to +1ing it.

-Sara

---

## #109343 — michal.brzuchalski@gmail.com — 2020-03-26T16:56:10+00:00

URL: https://externals.io/message/109335#109343 · In reply to: #109335

czw., 26 mar 2020 o 14:31 Nikita Popov <nikita.ppv@gmail.com> napisał(a):

> Hi internals,
>
> I would like to submit the following RFC for your consideration:
> https://wiki.php.net/rfc/constructor_promotion
>
> This is based on one off the suggestions made in
> https://externals.io/message/109220, and some existing discussion on the
> topic can be found in that thread.
>
>
As I already said in linked topic. IMHO this is wrong and can be easily
overused.

A list of properties is changed into a list of constructor method arguments
which
can be easily overused on long argument lists and when the time comes to
have attributes then the list of promoted arguments with their annotations
and some extra blank lines then to improve readability could lead to
long constructor signature list on a different level of indentation which
also then always has to be un-collapsed in IDE and on the top of a class to
keep the track of properties where usually list of properties exists with
additional named constructors (always at the top).

Not even mentioning that mix of promoted and un-promoted argument list
would just feel awkward and that kind of mixed constructor where some of
arguments are assigned magically and some of them not can make it harder to
read.

And I'm not saying this cause I'm preparing another proposal for filling
the gap
between the use of ad-hoc array structures, and the use of well-defined and
type-safe value types.
I'm saying this because PHP was always verbose and easy to read/decipher
this solution IMHO has more drawbacks regarding readability than benefits.

Cheers,
--
Michał Brzuchalski

---

## #109344 — Ben Ramsey — 2020-03-26T16:57:45+00:00

URL: https://externals.io/message/109335#109344 · In reply to: #109335

> 
> 
> Hi internals,
> 
> I would like to submit the following RFC for your consideration:
> https://wiki.php.net/rfc/constructor_promotion
> 
> This is based on one off the suggestions made in
> https://externals.io/message/109220, and some existing discussion on the
> topic can be found in that thread.
> 
> The primary motivation for this feature is to close the currently rather
> wide gap between the use of ad-hoc array structures, and the use of
> well-defined and type-safe value objects. The latter is what we want people
> to use, but the former is, unfortunately, what is *easy* to use.


Will this cause any problems with ReflectionClass? (I’m assuming not, but wanted to ask anyway.)

Cheers,
Ben

---

## #109348 — Nikita Popov — 2020-03-26T18:01:47+00:00

URL: https://externals.io/message/109335#109348 · In reply to: #109338

On Thu, Mar 26, 2020 at 5:02 PM David Rodrigues <david.proweb@gmail.com>
wrote:

> Hello!
>
> With all respect, seems a bit confuses to me, without in fact offer a new
> feature, just a shortcut. I hope it's me seeing it the wrong way.
>
> What happen if I rename the constructor parameters on a child class
> constructor?
>
> Example (https://3v4l.org/VqQde):
>
> class A {
>     public function __construct(public int $x) { ... }
> }
>
> class B extends A {
>     public function __construct(public int $y) { ...;
> parent::__construct($y); }
> }
>

Constructors (in general, independent of this feature) are per-class and do
not participate in classical signature-based inheritance (the exception
being abstract constructors, but those aren't relevant here). The notion of
"renaming" a constructor parameter doesn't make sense in that regard,
because the parameters of the child constructor have absolutely no relation
to the parameters of the parent constructor.

The way to write your example would be:

class A {
    public function __construct(public int $x) {}
}

class B extends A {
    public function __construct(int $x) {
        parent::__construct($x);
        // Your extra logic here.
    }
}

Regards,
Nikita

---

## #109368 — Stanislav Malyshev — 2020-03-26T23:56:44+00:00

URL: https://externals.io/message/109335#109368 · In reply to: #109335

Hi!

> I would like to submit the following RFC for your consideration:
> https://wiki.php.net/rfc/constructor_promotion

I like this shortcut. I am not sure though I understand why callable is
not allowed? Can't it just create a non-typed property?

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #109501 — Markus Fischer — 2020-04-01T23:33:28+00:00

URL: https://externals.io/message/109335#109501 · In reply to: #109335

Hi,

> I would like to submit the following RFC for your consideration:
> https://wiki.php.net/rfc/constructor_promotion
> 
> This is based on one off the suggestions made in
> https://externals.io/message/109220, and some existing discussion on the
> topic can be found in that thread.
> 
> The primary motivation for this feature is to close the currently rather
> wide gap between the use of ad-hoc array structures, and the use of
> well-defined and type-safe value objects. The latter is what we want people
> to use, but the former is, unfortunately, what is *easy* to use.

The amount of boilerplate this would cut down for e.g. projects using DI 
and DTOs is unfathomable.

How does this work together with reflection? Will reflection see the 
properties _and_ the extended constructor definitions?

thanks,
- Markus

---

## #109506 — Nikita Popov — 2020-04-02T10:07:36+00:00

URL: https://externals.io/message/109335#109506 · In reply to: #109501

> Hi,
> 
> > […]
> people
> > […]
>
> The amount of boilerplate this would cut down for e.g. projects using DI
> and DTOs is unfathomable.
>
> How does this work together with reflection? Will reflection see the
> properties _and_ the extended constructor definitions?
>

Reflection will see the state after desugaring. That is, it just sees
normal properties and normal constructor args. I've made this more explicit
in the RFC now: https://wiki.php.net/rfc/constructor_promotion#reflection

Nikita

---

## #109514 — Markus Fischer — 2020-04-02T13:45:32+00:00

URL: https://externals.io/message/109335#109514 · In reply to: #109506

Him


> Reflection will see the state after desugaring. That is, it just sees
> normal properties and normal constructor args. I've made this more explicit
> in the RFC now: https://wiki.php.net/rfc/constructor_promotion#reflection

Thanks!

Another one, also related a bit to your PHP-Parser (?): will the 
tokenization process "see" the code before desugaring?

I'm wondering how automated translation tool can detect that a certain 
code is not "sugared" and promote this?

thanks!
- Markus

---

## #109515 — Nikita Popov — 2020-04-02T13:52:13+00:00

URL: https://externals.io/message/109335#109515 · In reply to: #109514

> Him
>
> 
> > […]
> explicit
> > […]
> https://wiki.php.net/rfc/constructor_promotion#reflection
>
> Thanks!
>
> Another one, also related a bit to your PHP-Parser (?): will the
> tokenization process "see" the code before desugaring?
>
> I'm wondering how automated translation tool can detect that a certain
> code is not "sugared" and promote this?
>

Tokenization and parsing both happen before the desugaring, they see the
code in its original form. They would detect promoted properties by
checking whether a constructor parameter has a visibility modifier.  Of
course, tooling might want to perform the desugaring transformation
internally, so it sees a uniform representation of everything.

Regards,
Nikita

---

## #109527 — Mike Schinkel — 2020-04-04T07:51:16+00:00

URL: https://externals.io/message/109335#109527 · In reply to: #109515

Question: Will parse errors in the __construct() be reported as all being on the same line, or will the parser report each as a distinct line of code when reporting parse errors?


-Mike


> 
> 
> 
> 
> > […]
> 
> Tokenization and parsing both happen before the desugaring, they see the
> code in its original form. They would detect promoted properties by
> checking whether a constructor parameter has a visibility modifier.  Of
> course, tooling might want to perform the desugaring transformation
> internally, so it sees a uniform representation of everything.
> 
> Regards,
> Nikita

---

## #109562 — Nicolas Grekas — 2020-04-08T11:56:43+00:00

URL: https://externals.io/message/109335#109562 · In reply to: #109335

> I would like to submit the following RFC for your consideration:
> https://wiki.php.net/rfc/constructor_promotion
>

I love it :)

Just one question:
Shouldn't default values be copied on the signature? That would be expected
for me.
(and that would make the properties still initialized when child classes
don't call the parent constructor for whatever reasons.)

class Point {
    public function __construct(
        public float $x = 0.0,

<==>

class Point {
    public float $x = 0.0;

    public function __construct(
        float $x = 0.0,

Nicolas

---

## #109571 — Nikita Popov — 2020-04-08T14:18:28+00:00

URL: https://externals.io/message/109335#109571 · In reply to: #109562

On Wed, Apr 8, 2020 at 1:56 PM Nicolas Grekas <nicolas.grekas+php@gmail.com>
wrote:

>
> I would like to submit the following RFC for your consideration:
> > […]
>
> I love it :)
>
> Just one question:
> Shouldn't default values be copied on the signature? That would be
> expected for me.
> (and that would make the properties still initialized when child classes
> don't call the parent constructor for whatever reasons.)
>
> class Point {
>     public function __construct(
>         public float $x = 0.0,
>
> <==>
>
> class Point {
>     public float $x = 0.0;
>
>     public function __construct(
>         float $x = 0.0,
>
> Nicolas
>

Some reasons why the default value is not duplicated into the property are
discussed in https://wiki.php.net/rfc/constructor_promotion#desugaring.
It's primarily about forward-compatibility concerns.

But apart from that, I strongly believe that you should not, ever, specify
a default value on a property that is initialized in the constructor. You
can have a default, or you can have explicit initialization in the
constructor -- both together do not make sense.

Regards,
Nikita

---

## #109622 — Nikita Popov — 2020-04-14T13:02:31+00:00

URL: https://externals.io/message/109335#109622 · In reply to: #109335

> Hi internals,
>
> I would like to submit the following RFC for your consideration:
> https://wiki.php.net/rfc/constructor_promotion
>
> This is based on one off the suggestions made in
> https://externals.io/message/109220, and some existing discussion on the
> topic can be found in that thread.
>
> The primary motivation for this feature is to close the currently rather
> wide gap between the use of ad-hoc array structures, and the use of
> well-defined and type-safe value objects. The latter is what we want people
> to use, but the former is, unfortunately, what is *easy* to use.
>

As the reception has been positive, I plan to open voting on this proposal
soon. Please tell me if you have further concerns or the RFC needs
additional clarification.

Regards,
Nikita

---

## #109642 — Nikita Popov — 2020-04-14T20:02:25+00:00

URL: https://externals.io/message/109335#109642

> Hi Nikita,
>
> Personally, I don't see a big reason in introduction this syntax sugar.
> It allows to make class declaration more compact but less readable.
>
> Also, this feature doesn't work well with inheritance. RFC doesn't provide
> any examples with inherited classes.
> The implementation doesn't allow initialization of parent class
> properties, and doesn't allow to call parent constructor before
> initialization of child class properties (like C++ allows).
>

I have now added an example involving inheritance to the RFC:
https://wiki.php.net/rfc/constructor_promotion#inheritance

Constructor promotion does work with inheritance. As you pointed out, the
only oddity is that properties are assigned before the parent constructor
call, rather than after it. I don't think this makes a difference in
practice though.


> In my opinion, the existing PHP syntax is already clear and good enough.
>

Fair enough :)

Nikita

------------------------------
> *From:* Nikita Popov <nikita.ppv@gmail.com>
> *Sent:* Tuesday, April 14, 2020 16:02
> *To:* PHP internals <internals@lists.php.net>
> *Subject:* [PHP-DEV] Re: [RFC] Constructor Property Promotion
>
> 
>
> > […]
> people
> > […]
>
> As the reception has been positive, I plan to open voting on this proposal
> soon. Please tell me if you have further concerns or the RFC needs
> additional clarification.
>
> Regards,
> Nikita
>
>
> CAUTION: This email originated from outside of the organization. Do not
> click on links or open attachments unless you recognize the sender and know
> the content is safe.
>
> This e-mail may contain information that is privileged or confidential. If
> you are not the intended recipient, please delete the e-mail and any
> attachments and notify us immediately.

---

## #109876 — Nikita Popov — 2020-04-28T14:49:26+00:00

URL: https://externals.io/message/109335#109876 · In reply to: #109335

> Hi internals,
>
> I would like to submit the following RFC for your consideration:
> https://wiki.php.net/rfc/constructor_promotion
>
> This is based on one off the suggestions made in
> https://externals.io/message/109220, and some existing discussion on the
> topic can be found in that thread.
>
> The primary motivation for this feature is to close the currently rather
> wide gap between the use of ad-hoc array structures, and the use of
> well-defined and type-safe value objects. The latter is what we want people
> to use, but the former is, unfortunately, what is *easy* to use.
>

As it looks like the Attributes RFC is going to be accepted, we need to
consider how it will interact with this proposal. I've added an extra
section for this: https://wiki.php.net/rfc/constructor_promotion#attributes

The problem is that attributes are allowed both on properties and on
parameters. For promoted properties, what does the attribute apply to? My
original suggestion was to make it apply to both the parameter and the
generated property. However, Benjamin pointed out that an attribute on a
promoted property is almost certainly intended to apply to the property
only, especially because parameter attributes have not been supported
historically (in phpdoc). Applying it to both the property and the
parameter may cause issues for attributes that want to strictly validate
where they are used.

I think the best answer to this question may be to forbid the use of
attributes on promoted properties entirely, because there is no unambiguous
interpretation for them. I also think that using attributes pushes this
"syntax sugar" to its limit, as you can easily end up with a 50 line
constructor signature that way, at least if I'm going by some of the more
involved uses of annotations...

Does anyone have thoughts on this?

Regards,
Nikita

---

## #109878 — Nicolas Grekas — 2020-04-28T15:17:22+00:00

URL: https://externals.io/message/109335#109878 · In reply to: #109876

> 
>
> > […]
> people
> > […]
>
> As it looks like the Attributes RFC is going to be accepted, we need to
> consider how it will interact with this proposal. I've added an extra
> section for this:
> https://wiki.php.net/rfc/constructor_promotion#attributes
>
> The problem is that attributes are allowed both on properties and on
> parameters. For promoted properties, what does the attribute apply to? My
> original suggestion was to make it apply to both the parameter and the
> generated property. However, Benjamin pointed out that an attribute on a
> promoted property is almost certainly intended to apply to the property
> only, especially because parameter attributes have not been supported
> historically (in phpdoc). Applying it to both the property and the
> parameter may cause issues for attributes that want to strictly validate
> where they are used.
>
> I think the best answer to this question may be to forbid the use of
> attributes on promoted properties entirely, because there is no unambiguous
> interpretation for them. I also think that using attributes pushes this
> "syntax sugar" to its limit, as you can easily end up with a 50 line
> constructor signature that way, at least if I'm going by some of the more
> involved uses of annotations...
>

This last proposal would kinda defeat constructor promotion and all the
reasons why it was introduced in the first place.

I would expect attributes to apply only to the properties on my side.

If people want different or duplicate attributes on parameters, then it's
normal to not be able to use constructor promotion.

My 2cts,
Nicolas

---

## #109882 — Ilija Tovilo — 2020-04-28T15:45:35+00:00

URL: https://externals.io/message/109335#109882 · In reply to: #109878

Hi Nicolas

> I would expect attributes to apply only to the properties on my side.

Parameter annotations could be interesting for dependency injection.
Symfony currently has some DI magic through parameter names among
other things. It might be nice to control these through annotations
instead. This would be impossible or involve a lot of hackery and
assumptions if the annotations aren't available on the parameters.
Alternatively we might make this information available through
reflection (like ReflectionParameter::getGeneratedProperty()) so you
can look for the annotation there.

I don't think this is a huge problem, just wanted to mention it.

Ilija

---

## #109883 — Nicolas Grekas — 2020-04-28T15:56:11+00:00

URL: https://externals.io/message/109335#109883 · In reply to: #109882

>
> > […]
>
> Parameter annotations could be interesting for dependency injection.
> Symfony currently has some DI magic through parameter names among
> other things. It might be nice to control these through annotations
> instead. This would be impossible or involve a lot of hackery and
> assumptions if the annotations aren't available on the parameters.
> Alternatively we might make this information available through
> reflection (like ReflectionParameter::getGeneratedProperty()) so you
> can look for the annotation there.
>

Sure, this could be considered.

But this doesn't contradict what I wrote:
if one uses constructor promotion, the then attributes should only go to
the properties to me.
If one wants to add attributes to parameters, then one would need to
opt-out from constructor promotion.

Nicolas

---

## #109896 — Larry Garfield — 2020-04-29T00:04:30+00:00

URL: https://externals.io/message/109335#109896 · In reply to: #109883

> > […]
> 
> Sure, this could be considered.
> 
> But this doesn't contradict what I wrote:
> if one uses constructor promotion, the then attributes should only go to
> the properties to me.
> If one wants to add attributes to parameters, then one would need to
> opt-out from constructor promotion.
> 
> Nicolas

I'm inclined to agree with Nicolas.  My gut feeling (and I don't have more data to back it up than that, I admit) is that property annotations will be more prevalent than parameter annotations, so those should take precedence.

That said... how many will conflict?  In practice, the most common use case I can see for parameter attributes would be fancy DI configuration for services... and I don't know that I'd care about attributes on the properties in that case.  Conversely, for properties that have attributes that will usually be for extended type information, ORM mapping, and stuff like that.  For which... I can't think of what I'd want attributes I'd want on the parameter itself.

Perhaps I'm just not creative enough at this hour, but I'm not sure that "both" is such a problem.  It only seems like it would be an issue for attributes that user-space wanted to enforce on certain cases only; since attributes are new, a viable answer there is "btw, if you're restricting your annotation be aware of this double-case and either allow it or don't, but it's on you to decide what if anything to do."

--Larry Garfield

---

## #109904 — Nikita Popov — 2020-04-29T07:33:56+00:00

URL: https://externals.io/message/109335#109904 · In reply to: #109896

On Wed, Apr 29, 2020 at 2:05 AM Larry Garfield <larry@garfieldtech.com>
wrote:

> 
> > […]
>
> I'm inclined to agree with Nicolas.  My gut feeling (and I don't have more
> data to back it up than that, I admit) is that property annotations will be
> more prevalent than parameter annotations, so those should take precedence.
>
> That said... how many will conflict?  In practice, the most common use
> case I can see for parameter attributes would be fancy DI configuration for
> services... and I don't know that I'd care about attributes on the
> properties in that case.  Conversely, for properties that have attributes
> that will usually be for extended type information, ORM mapping, and stuff
> like that.  For which... I can't think of what I'd want attributes I'd want
> on the parameter itself.
>
> Perhaps I'm just not creative enough at this hour, but I'm not sure that
> "both" is such a problem.  It only seems like it would be an issue for
> attributes that user-space wanted to enforce on certain cases only; since
> attributes are new, a viable answer there is "btw, if you're restricting
> your annotation be aware of this double-case and either allow it or don't,
> but it's on you to decide what if anything to do."
>

That's basically my line of thinking as well. I do agree that as things are
right now, most likely a property annotation is intended. But that's
potentially only an artifact of phpdoc annotations not supporting parameter
annotations, so there is no existing ecosystem around them.

I think it might be best to apply to "both" and provide an isPromoted()
method on both ReflectionParameter and ReflectionProperty. Any code that
wishes to validate the allowed positions of an attribute can then skip
properties/parameters that report isPromoted() as true, to avoid reporting
false positives.

Nikita

---

Next page: https://externals.io/message/109335.md?page=2
