> On Mar 26, 2020, at 9:30 AM, Nikita Popov <nikita....@gmail.com> 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.
> 
> Regards,
> Nikita

I am rather concerned about constructor promotion will result in some complex 
"single" lines of code that all must be syntactically correct for any of it to 
be syntactically correct.  It will make it more challenging to read and write 
code as we add attributes and PHP doc to properties.  And if we still have to 
deal with concern that parameter names that were previously never indented to 
become part of the API will be exposed as part of the API, with potential 
breakage when name changes.

Pondering these issues I think I have a very simple proposal that would address 
most (all?) of the concerns I am aware for both Constructor Property Promotion 
and Named Parameters.  

Consider simply what we might call "Parameter Blocks."  Since PHP always 
expects a parentheses to follow the function or method name it should be 
possible to opt-in replace it with a brace-enclosed block of parameters instead 
since it would be unambiguous and this no conflict, and no need for new 
keywords.  

The following illustrates how it my be used by repurposing an example from the 
RFC:

abstract class Node {
    public function __construct{
        /**
         * @param Location|null — The starting location for this abstract node
         */
        Location $startLoc = null;

        protected Location $startLoc = null;

        /**
         * @param Location|null — The ending location for this abstract node
         */
        protected Location $endLoc = null;
    } {}
}
 
class ParamNode extends Node {
    public function __construct{
        /**
         * @param string $name — Names this Node.
         */
        public string $name;

        /**
         * @param ExprNode|null $default — Contains the expression this node 
represents 
         */
        <<ORM\Entity("exprNode")>>
        public ExprNode $default = null;

        /**
         * @param TypeNode|null $type — Contains the type expected for this 
node's expression
         */
        <<ORM\Entity("typeNode")>>
        public TypeNode $type = null;

        /**
         * @param bool $byRef — True if this node is a reference, false 
otherwise
         */
        public bool $byRef = false;

        /**
         * @param bool $byRef — True if this node is variadic, false otherwise
         */
        public bool $variadic = false;

        /**
         * @param Location|null — The starting location for this node
         */
        Location $startLoc = null;

        /**
         * @param Location|null — The ending location for this node
         */
        Location $endLoc = null;
    } {
        parent::__construct($startLoc, $endLoc);
    }
}

Envision how that same code would have to be written if all as part of the 
single "line" contained within the parenthesis of a programming language?  

As far as I can tell this is a clean and elegant approach with many fewer 
downsides than those that have been discussed thus far.  I hope that each of 
you reading this will seriously consider this is viable alternative option.

-Mike

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to