rfc:code_free_constructor

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
rfc:code_free_constructor [2019/01/14 14:52] – created rjhdbyrfc: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, rjhdby@php.net   * Author: Andrey Gromov, andrewgrom@rambler.ru, rjhdby@php.net
   * Proposed version: PHP 8   * Proposed version: PHP 8
-  * Status: Draft+  * Status: draft
   * First Published at: https://wiki.php.net/rfc/code_free_constructor   * First Published at: https://wiki.php.net/rfc/code_free_constructor
   * ML thread: -   * ML thread: -
  
 ===== Introduction ===== ===== Introduction =====
-Mainly "code free" constructors used for declaration of DTO-classes. +"Code free" constructor is constructor with only purpose to directly set object properties from received parameters and, optionally, call parent constructor. 
-Also, some developers believe that it is good OOP-practice. + 
-Unfortunately they looks like unnecessary boilerplate.+They used for
 +  * for DTO-classes declaration 
 +  developersthat believe it is good OOP-practice, use them everywhere 
 +  * 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  = $first+    public $cc; 
-        $this->second = $second;+    public $whells = 2; 
 +     
 +    public function __construct($vendor, $cc) { 
 +        $this->vendor = $vendor
 +        $this->cc     = $cc; 
 +    } 
 +     
 +    //other methods 
 +
 + 
 +class MyCustomMotorCycle extends MotorCycle { 
 +    public function __construct($cc, $whells) { 
 +        parent::__construct("Custom", $cc); 
 +     // $this->cc = $cc;  this statement will be added within proposed realisation 
 +        $this->whells = $whells;
     }     }
-    ... 
 } }
 </code> </code>
Line 28: Line 47:
 Proposed syntax: Proposed syntax:
 <code php> <code php>
-class Test($first, $second){ +class MotorCycle($vendor, $cc){ 
-    ...+    public $whells = 2; 
 +     
 +    //other methods 
 +}; 
 + 
 +class MyCustomMotorCycle($cc, $whells) extends MotorCycle("Custom", $cc){ }; 
 +</code> 
 + 
 +{{:rfc:joined.png?400|}} 
 + 
 +By the way, current 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 wayYou 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. 
 + 
 +<code php> 
 +class MyCustomMotorCycle(int $cc, int $whells = 3, ...$otherParams) extends MotorCycle("Custom", $cc){ }; 
 +</code> 
 + 
 +===== 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's properties with same name. 
 +<code php> 
 +class Parent{ 
 +    public function __construct($prop){ 
 +        $this->prop = $prop * 2; 
 +    } 
 +
 +class Child($prop) extends Parent($prop) {}; 
 + 
 +$child = new Child(5); 
 + 
 +var_dump($child); 
 + 
 +---------------- 
 + 
 +object(Child) { 
 +    ["prop"] => 5 // instead expected 10
 } }
 </code> </code>
  
 ===== 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's constructor call+ 
 +Maybe simplified "destructuring declaration" for those class types? Not sure. 
 + 
 +<code php> 
 +class A($first, $second) {}; 
 +$a = new A(1,2); 
 +[$first, $second] = $a; 
 +</code>
  
 ===== Implementation ===== ===== Implementation =====
 +Implemented via injecting generated "__construct"'s ast node into class statements list. So it will be compiled with all checks.
 +
 Draft implementation, need to be reviewed. Draft implementation, need to be reviewed.
-https://github.com/php/php-src/compare/master...rjhdby:constructor?expand=1+https://github.com/rjhdby/php-src/compare/master...rjhdby:constructor
  
 ===== References ===== ===== References =====
  
  
rfc/code_free_constructor.txt · Last modified: 2019/01/29 12:17 by rjhdby