Re: [RFC] Parameter Blocks (vs. Constructor Property Promotion and Named Parameters)
| From: | Larry Garfield | Date: | Fri, 08 May 2020 18:43:46 +0000 |
| Subject: | Re: [RFC] Parameter Blocks (vs. Constructor Property Promotion and Named Parameters) | ||
| References: | 1 2 3 4 5 6 7 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-110091@lists.php.net to get a copy of this message | ||
On Fri, May 8, 2020, at 1:04 PM, Rowan Tommins wrote:
> On 08/05/2020 02:32, Mike Schinkel wrote:
> > 8. I believe this alternate syntax addresses Michal Bruzuchalski's and Larry
> > Garfield's concerns...
> > 10. Nikita claims there is a "line" to be crossed when property declarations
> > have their own blocks and would disallow their use with CPP...
>
>
> I hesitate to speak for them, but I think their concerns are more
> conceptual: that this requires nesting a large portion of the class
> definition inside the constructor definition. Changing the syntax
> doesn't really change that - the "parameter block" would still be an
> extra level of nesting, attached to the constructor definition, rather
> than at the top level of the class, where it lives today.
Correct. My concern isn't with comma vs semicolon. My concern is that the property definition
is then living between
public function __construct( and ) {.
When the property definition is basic (visibility, type, and something like readonly) that's
still reasonable enough for the benefit it brings.
When the property definition becomes involved enough that it is preferable to have it span lines
(docblock, attributes, or something like property accessors or potentially even just asymmetric
visibility), that's when I get fidgety, because then you're technically writing
public function __construct(/** @see blah.html */ <<Positive>> protected int $count, /**
@see blah.html */ <<Positive>> <<LessThan(99)>> protected int $age,) {
}
Just with convenience newlines. Semi-colons and curly braces wouldn't in themselves address
that.
What *might* would be putting promotable properties in an entirely separate construct from the
constructor.
An off the cuff riff from that might be something like:
class Stuff {
promoted {
/**
* @see blah.html
*/
<<Positive>>
protected int $count;
/**
* @see blah.html
*/
<<Positive>>
<<LessThan(99)>>
protected int $age;
}
}
That moves them out of the constructor entirely, and might overlap with the desire for named
parameters.
$p = new Stuff(5, 30);
// or
$p = new Stuff{count: 5, age: 30};
However, that has the major drawback of then not being in the constructor, so promoting only some
constructor arguments becomes impossible. It's also worse on the BC front.
I don't know if that's a good idea or not; as I said, just an off-the-cuff riff on what
would address the "complex properties" question better. For the simple case, the current
RFC syntax seems fine. (Maybe someone can riff off of this to something better.)
--Larry Garfield