rfc:code_free_constructor
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:code_free_constructor [2019/01/23 12:09] – rjhdby | rfc:code_free_constructor [2019/01/29 12:17] (current) – 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) { | + | |
- | parent:: | + | |
- | // $this-> | + | |
- | $this->whells | + | parent:: |
+ | $this->passenger | ||
} | } | ||
} | } | ||
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 silently 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 87: | Line 112: | ||
Not thinking so | Not thinking so | ||
- | ===== Future Scope ===== | + | ===== Implementation |
- | Maybe simplified " | + | Draft implementation, |
+ | https:// | ||
- | <code php> | + | ===== Q&A, Discussion ===== |
- | class A($first, $second) {}; | + | **Stanislav Malyshev**< |
- | $a = new A(1,2); | + | |
- | [$first, $second] = $a; | + | |
- | </code> | + | |
- | ===== Implementation ===== | + | **Nikita Popov**< |
- | Implemented via injecting generated " | + | |
+ | https:// | ||
+ | => Proposed function public function _ _ construct(int $this-> | ||
- | Draft implementation, | + | https://docs.hhvm.com/hack/other-features/ |
- | https://github.com/php/php-src/compare/ | + | => 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. IMHO this syntax make behavior more strict. No need to | ||
+ | read constructor' | ||
===== References ===== | ===== References ===== | ||
rfc/code_free_constructor.txt · Last modified: 2019/01/29 12:17 by rjhdby