rfc:code_free_constructor
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:code_free_constructor [2019/01/17 13:09] – rjhdby | rfc:code_free_constructor [2019/01/29 12:13] – rjhdby | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== PHP RFC: Code free constructor ====== | ====== 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: draft |
* 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 ===== | ||
Line 20: | Line 28: | ||
===== 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 MotorCycle { | class MotorCycle { | ||
- | | + | |
- | public $cc; | + | public $driver; |
- | | + | |
| | ||
- | public function __construct($vendor, | + | public function __construct(string |
$this-> | $this-> | ||
+ | $this-> | ||
$this-> | $this-> | ||
} | } | ||
- | | ||
- | //other methods | ||
} | } | ||
+ | </ | ||
+ | Proposed syntax: | ||
+ | <code php> | ||
+ | class MotorCycle(protected string $vendor, Person $driver, private int $cc = 600){ }; | ||
+ | </ | ||
+ | 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 { | class MyCustomMotorCycle extends MotorCycle { | ||
- | public function __construct($cc, $whells) { | + | |
- | // $this-> | + | |
- | $this-> | + | |
- | parent:: | + | parent:: |
+ | $this-> | ||
} | } | ||
} | } | ||
Line 47: | Line 73: | ||
Proposed syntax: | Proposed syntax: | ||
<code php> | <code php> | ||
- | class MotorCycle($vendor, $cc){ | + | class MyCustomMotorCycle(Person |
- | | + | extends MotorCycle(" |
- | + | ||
- | //other methods | + | |
- | }; | + | |
- | + | ||
- | class MyCustomMotorCycle($cc, | + | |
</ | </ | ||
+ | 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. | ||
- | ===== Possible Issue ===== | + | Realisation is similar to described above with additional checks and parent call. |
- | Child constructor will rewrite parent' | + | Note that you can use all standard syntax constructions like default parameters and splat operator ("...") |
- | <code php> | + | |
- | class Parent{ | + | |
- | public function __construct($prop){ | + | |
- | $this-> | + | |
- | } | + | |
- | } | + | |
- | class Child($prop) extends Parent($prop) {}; | + | |
- | $child = new Child(5); | + | {{: |
- | var_dump($child); | + | ===== 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-> | ||
+ | } | ||
+ | }; | ||
- | object(Child) { | + | // |------arguments------||-----------constructor params------------| |
- | ["prop"] => 5 // instead expected 10 | + | $a = new class($mDriver, $mPassenger)(Person $driver, private Person $passenger) |
- | } | + | |
</ | </ | ||
+ | |||
+ | {{: | ||
===== Backward Incompatible Changes ===== | ===== Backward Incompatible Changes ===== | ||
Line 86: | Line 111: | ||
===== RFC Impact ===== | ===== RFC Impact ===== | ||
Not thinking so | Not thinking so | ||
- | |||
- | ===== Future Scope ===== | ||
===== Implementation ===== | ===== Implementation ===== | ||
- | Implemented via injecting generated " | ||
- | Draft implementation, | + | Draft implementation, |
- | https:// | + | 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