rfc:code_free_constructor
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:code_free_constructor [2019/01/14 14:52] – created rjhdby | rfc:code_free_constructor [2019/01/29 12:13] – rjhdby | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Your Title Here ====== | + | ====== PHP RFC: Code free constructor |
- | * Version: 0.1 | + | * Version: 0.2 |
- | * Date: 2019-01-14 | + | * Date: 2019-01-29 |
* Author: Andrey Gromov, andrewgrom@rambler.ru, | * Author: Andrey Gromov, andrewgrom@rambler.ru, | ||
* Proposed version: PHP 8 | * Proposed version: PHP 8 | ||
- | * Status: | + | * Status: |
* First Published at: https:// | * First Published at: https:// | ||
- | * ML thread: | + | * ML thread: |
+ | |||
+ | ===== Changelog ===== | ||
+ | **v 0.2** | ||
+ | * Added visibility modificators syntax. | ||
+ | * Changed behavior for parameters that need to be forwarded to parent. | ||
+ | * Added " | ||
+ | * Added " | ||
+ | * Improved description of realization. | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | Mainly | + | "Code free" |
- | Also, some developers | + | |
- | Unfortunately | + | They used for: |
+ | * for DTO-classes | ||
+ | | ||
+ | * useful in some cases of inheritance | ||
+ | |||
+ | Unfortunately, php syntax enforces to write unnecessary boilerplate. | ||
===== Proposal ===== | ===== Proposal ===== | ||
Proposal is to add alternate syntax for "code free" constructors. | Proposal is to add alternate syntax for "code free" constructors. | ||
+ | |||
+ | There is two generals parts of proposal. | ||
+ | |||
+ | === First. Automatic property initialization === | ||
+ | |||
Current syntax: | Current syntax: | ||
<code php> | <code php> | ||
- | class Test{ | + | class MotorCycle |
- | public function __construct($first, $second){ | + | protected $vendor; |
- | $this->first | + | public $driver; |
- | $this->second | + | private int $cc; |
+ | | ||
+ | public function __construct(string | ||
+ | $this->vendor = $vendor; | ||
+ | $this-> | ||
+ | $this->cc = $cc; | ||
} | } | ||
- | ... | ||
} | } | ||
</ | </ | ||
Line 28: | Line 50: | ||
Proposed syntax: | Proposed syntax: | ||
<code php> | <code php> | ||
- | class Test($first, $second){ | + | class MotorCycle(protected string |
- | ... | + | </ |
+ | If no visibility modifier defined it casts as " | ||
+ | |||
+ | Under the hood, at AST-creation phase, will be silently created right properties declaration and added "_ _ construct" | ||
+ | |||
+ | === Second. Automatic passing args to parent constructor === | ||
+ | For this example I will use class " | ||
+ | |||
+ | Current syntax: | ||
+ | <code php> | ||
+ | class MyCustomMotorCycle extends MotorCycle { | ||
+ | public $passenger; | ||
+ | |||
+ | public function __construct(Person $driver, Person $passenger) { | ||
+ | parent:: | ||
+ | $this-> | ||
+ | } | ||
} | } | ||
</ | </ | ||
+ | |||
+ | Proposed syntax: | ||
+ | <code php> | ||
+ | class MyCustomMotorCycle(Person $driver, Person $passenger) | ||
+ | extends MotorCycle(" | ||
+ | </ | ||
+ | In this case, if some params must be forwarded to parent then they will not be declared and set in child. This behavior is disputable. | ||
+ | |||
+ | Realisation is similar to described above with additional checks and parent call. | ||
+ | |||
+ | Note that you can use all standard syntax constructions like default parameters and splat operator (" | ||
+ | |||
+ | {{: | ||
+ | |||
+ | ===== Anonymous classes ===== | ||
+ | Since the syntax for declaring an anonymous class is what it is, the only option that I can imagine is not very beautiful. | ||
+ | |||
+ | <code php> | ||
+ | $a = new class($mDriver, | ||
+ | private $passenger; | ||
+ | public function __construct(Person $driver, Person $passenger) { | ||
+ | parent:: | ||
+ | $this-> | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | // |------arguments------||-----------constructor params------------| | ||
+ | $a = new class($mDriver, | ||
+ | extends MotorCycle(" | ||
+ | </ | ||
+ | |||
+ | {{: | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | No. | + | Do not know. Looks like no BI. |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
Line 41: | Line 111: | ||
===== RFC Impact ===== | ===== RFC Impact ===== | ||
Not thinking so | Not thinking so | ||
- | |||
- | ===== Future Scope ===== | ||
- | Deal with parent' | ||
===== Implementation ===== | ===== Implementation ===== | ||
- | Draft implementation, | ||
- | https:// | ||
+ | Draft implementation, | ||
+ | https:// | ||
+ | |||
+ | ===== Q&A, Discussion ===== | ||
+ | **Stanislav Malyshev**< | ||
+ | |||
+ | **Nikita Popov**< | ||
+ | |||
+ | https:// | ||
+ | => Proposed function public function _ _ construct(int $this-> | ||
+ | |||
+ | https:// | ||
+ | => Uses public function _ _ construct(public int $x, public int $y) {} to declare properties in-line in the constructor. | ||
+ | |||
+ | I think that *if* we want to add some kind of sugar of this type, then I'd strongly prefer the syntax used by Hack than the one proposed here. It makes a lot more sense to me intuitively, | ||
+ | A matter of habit and documentation. There is a lot of really strange and magical behavior in the language. It seems to me that the proposed concept is quite simple and transparent for understanding. | ||
===== References ===== | ===== References ===== | ||
rfc/code_free_constructor.txt · Last modified: 2019/01/29 12:17 by rjhdby