Re: [PHP-DEV] [RFC] Constructor Property Promotion
On Wed, Apr 8, 2020 at 1:56 PM Nicolas Grekas wrote: > > 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 > 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
Re: [PHP-DEV] [RFC] Constructor Property Promotion
> 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
Re: [PHP-DEV] [RFC] Constructor Property Promotion
On Thu, Apr 2, 2020 at 3:47 PM Markus Fischer wrote: > Him > > On 02.04.20 12:07, Nikita Popov wrote: > > 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? > 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
Re: [PHP-DEV] [RFC] Constructor Property Promotion
Him On 02.04.20 12:07, Nikita Popov wrote: 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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] [RFC] Constructor Property Promotion
On Thu, Apr 2, 2020 at 1:35 AM Markus Fischer wrote: > Hi, > On 2020-03-26 14:30, Nikita Popov wrote: > > 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? > 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
Re: [PHP-DEV] [RFC] Constructor Property Promotion
Hi, On 2020-03-26 14:30, Nikita Popov wrote: 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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] [RFC] Constructor Property Promotion
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 smalys...@gmail.com -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] [RFC] Constructor Property Promotion
On Thu, Mar 26, 2020 at 5:02 PM David Rodrigues 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
Re: [PHP-DEV] [RFC] Constructor Property Promotion
> On Mar 26, 2020, at 08:30, Nikita Popov wrote: > > 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 signature.asc Description: Message signed with OpenPGP
Re: [PHP-DEV] [RFC] Constructor Property Promotion
czw., 26 mar 2020 o 14:31 Nikita Popov 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
Re: [PHP-DEV] [RFC] Constructor Property Promotion
On Thu, Mar 26, 2020 at 8:31 AM Nikita Popov wrote: > 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
Re: [PHP-DEV] [RFC] Constructor Property Promotion
On Thu, Mar 26, 2020 at 9:33 AM Paul M. Jones wrote: > > > > On Mar 26, 2020, at 08:30, Nikita Popov wrote: > > > > 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. > > 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 > pmjo...@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 > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Chase Peeler chasepee...@gmail.com
Re: [PHP-DEV] [RFC] Constructor Property Promotion
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 escreveu: > 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. > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >
Re: [PHP-DEV] [RFC] Constructor Property Promotion
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. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP-DEV] [RFC] Constructor Property Promotion
> On Mar 26, 2020, at 08:30, Nikita Popov wrote: > > 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 pmjo...@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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php