Re: Proposal fo "Code-free constructors declaration"
| From: | Andrey O Gromov | Date: | Wed, 23 Jan 2019 13:21:59 +0000 |
| Subject: | Re: Proposal fo "Code-free constructors declaration" | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-103796@lists.php.net to get a copy of this message | ||
>> Full description
>> https://wiki.php.net/rfc/code_free_constructor
>> Draft realisation
https://github.com/php/php-src/compare/master...rjhdby:constructor
>>
>> "Code free" constructor is constructor with only purpose to directly
set
>> object properties from received parameters and, optionally, call parent
>> constructor.
>>
>> Main idea is to move such constructor declaration inside class
>> declaration.
> I don't like the approach but I can change my mind. Currently, I see an
> issue with your RFC: How do you add types with this syntax? Given this
> syntax from the RFC:
>
>class MotorCycle($vendor, $cc){
> public $whells = 2;
>};
>
>class MyCustomMotorCycle($cc, $whells) extends MotorCycle("Custom", $cc){
};
>
>How do you say that
public $cc can be an integer for instance,
>so
>expressing this public int $cc?
My realization simply add "__construct" method into class via AST
injection.
Another words, code "($cc, $whells)" considered as zend_ast node
"parameter_list" and
accordingly processed by standart way. You can declare property type
like you declare them inside standard method.
Also you can declare defaults for parameters, use "..." notation (there is
a nuance)
and do everything else.
class MyCustomMotorCycle(int $cc, int $whells = 3, ...$otherParams)
extends MotorCycle("Custom", $cc){ };