rfc:disallow-multiple-constructor-calls

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:disallow-multiple-constructor-calls [2017/01/17 20:02] – Added Title fleshgrinderrfc:disallow-multiple-constructor-calls [2021/03/27 15:00] (current) – Move to inactive ilutov
Line 1: Line 1:
 ====== PHP RFC: Disallow Multiple Constructor Calls ====== ====== PHP RFC: Disallow Multiple Constructor Calls ======
-  * Version: 0.1+  * Version: 0.2
   * Date: 2017-01-17   * Date: 2017-01-17
   * Author: Richard Fussenegger <php@fleshgrinder.com>, Tim Behashvyly <tim.bezhashvyly@gmail.com>   * Author: Richard Fussenegger <php@fleshgrinder.com>, Tim Behashvyly <tim.bezhashvyly@gmail.com>
-  * Status: Draft+  * Status: Inactive
   * First Published at: https://wiki.php.net/rfc/disallow-multiple-constructor-calls   * First Published at: https://wiki.php.net/rfc/disallow-multiple-constructor-calls
  
Line 91: Line 91:
 </code> </code>
  
-As illustrated, the functionality allows breaking of the encapsulation of objects at runtime. It is true that there are many ways to achieve the same thing and that the likelihood that a developer does anything like the above by accident. But there is also no argument why this requires support other than misusing the constructor of a class for things it was never intended to be used for. This is most apparent with <html>parent::__construct()</html> calls from child classes where the PHP language specification states that:+As illustrated, the functionality allows breaking of the encapsulation of objects at runtime. It is true that there are many ways to achieve the same thing and that the likelihood that a developer does anything like the above by accident is very low. But there is also no argument why this requires support other than misusing the constructor of a class for things it was never intended to be used for. This is most apparent with <html>parent::__construct()</html> calls from child classes where the PHP language specification states that:
  
 > //A constructor should not call its base-class constructor more than once.// > //A constructor should not call its base-class constructor more than once.//
Line 99: Line 99:
 Leaving the problem to the developers themselves. It is possible for developers to protect their objects against such unintended usage by asserting that all properties are null but this is unnecessary boilerplate code in a language that is already very verbose. Leaving the problem to the developers themselves. It is possible for developers to protect their objects against such unintended usage by asserting that all properties are null but this is unnecessary boilerplate code in a language that is already very verbose.
  
-We propose that multiple calls to the constructor of an object should result in an error instead of breaking encapsulation. This means in effect that the only idiomatic way to create a new instance is via the new keyword. Child classes are only permitted to call their parent constructor once and further calls are going to result in an error too.+We propose that multiple calls to the constructor of an object should result in an error instead of breaking encapsulation. This means in effect that the only idiomatic way to create a new instance is via the ''new'' keyword. Child classes are only permitted to call their parent constructor once and further calls are going to result in an error too.
  
-This means in effect that the code examples posted earlier would result in errors, however, another code example that posted on internals as a legitimate use case for calling the constructor method directly would continue to work as is:+This means in effect that the code examples posted earlier would result in errors, however, another code example that was posted on internals as a legitimate use case for calling the constructor method directly would continue to work as is:
  
 <code php> <code php>
Line 150: Line 150:
   * Java does not support multiple calls (compiler error).   * Java does not support multiple calls (compiler error).
   * JavaScript, Python, and Ruby allow multiple calls.   * JavaScript, Python, and Ruby allow multiple calls.
 +
 +==== Upgrade Paths ====
 +We propose to include this change either in the next feature release or the next major. The decision is up to the voters. However, we propose that an error with severity ''E_DEPRECATED'' should be emitted upon multiple calls to a constructor if the vote’s result is to include the change in the next major release. This is to ensure that all users notice this change and are able to upgrade their code accordingly.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
rfc/disallow-multiple-constructor-calls.1484683341.txt.gz · Last modified: 2017/09/22 13:28 (external edit)