rfc:code_free_constructor
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:code_free_constructor [2019/01/14 14:52] – created rjhdby | rfc:code_free_constructor [2019/01/28 07:18] – rjhdby | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== PHP RFC: Your Title Here ====== | + | ====== PHP RFC: Code free constructor |
* Version: 0.1 | * Version: 0.1 | ||
* Date: 2019-01-14 | * Date: 2019-01-14 | ||
* 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: - | ||
===== 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. | ||
+ | |||
Current syntax: | Current syntax: | ||
<code php> | <code php> | ||
- | class Test{ | + | class MotorCycle |
- | public function __construct($first, $second){ | + | public $vendor; |
- | $this->first | + | public $cc; |
- | $this->second | + | public $whells = 2; |
+ | | ||
+ | public function __construct($vendor, $cc) { | ||
+ | $this->vendor | ||
+ | $this->cc = $cc; | ||
+ | } | ||
+ | |||
+ | //other methods | ||
+ | } | ||
+ | |||
+ | class MyCustomMotorCycle extends MotorCycle { | ||
+ | public function __construct($cc, | ||
+ | parent:: | ||
+ | // $this-> | ||
+ | $this-> | ||
} | } | ||
- | ... | ||
} | } | ||
</ | </ | ||
Line 28: | Line 47: | ||
Proposed syntax: | Proposed syntax: | ||
<code php> | <code php> | ||
- | class Test($first, $second){ | + | class MotorCycle($vendor, $cc){ |
- | ... | + | |
+ | |||
+ | //other methods | ||
+ | }; | ||
+ | |||
+ | class MyCustomMotorCycle($cc, | ||
+ | </ | ||
+ | |||
+ | {{: | ||
+ | |||
+ | By the way, current realization simply add "_ _construct" | ||
+ | Another words, code "($cc, $whells)" | ||
+ | |||
+ | <code php> | ||
+ | class MyCustomMotorCycle(int $cc, int $whells = 3, ...$otherParams) extends MotorCycle(" | ||
+ | </ | ||
+ | |||
+ | ===== Restrictions ===== | ||
+ | This syntax can't be used with anonymous classes because those classes instantiated at once they declared and same syntax is used for forwarding parameters directly into constructor. | ||
+ | |||
+ | ===== Possible Issue ===== | ||
+ | |||
+ | Child constructor will silently rewrite parent' | ||
+ | <code php> | ||
+ | class Parent{ | ||
+ | public function __construct($prop){ | ||
+ | $this-> | ||
+ | } | ||
+ | } | ||
+ | class Child($prop) extends Parent($prop) {}; | ||
+ | |||
+ | $child = new Child(5); | ||
+ | |||
+ | var_dump($child); | ||
+ | |||
+ | ---------------- | ||
+ | |||
+ | object(Child) { | ||
+ | [" | ||
} | } | ||
</ | </ | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
- | No. | + | Do not know. Looks like no BI. |
===== Proposed PHP Version(s) ===== | ===== Proposed PHP Version(s) ===== | ||
Line 43: | Line 100: | ||
===== Future Scope ===== | ===== Future Scope ===== | ||
- | Deal with parent' | + | |
+ | Maybe simplified " | ||
+ | |||
+ | <code php> | ||
+ | class A($first, $second) {}; | ||
+ | $a = new A(1,2); | ||
+ | [$first, $second] = $a; | ||
+ | </ | ||
===== Implementation ===== | ===== Implementation ===== | ||
+ | Implemented via injecting generated " | ||
+ | |||
Draft implementation, | Draft implementation, | ||
- | https:// | + | https:// |
===== References ===== | ===== References ===== | ||
rfc/code_free_constructor.txt · Last modified: 2019/01/29 12:17 by rjhdby