rfc:inheritance_private_methods

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
rfc:inheritance_private_methods [2020/04/16 19:57]
pmmaga
rfc:inheritance_private_methods [2020/07/17 14:59] (current)
pmmaga
Line 3: Line 3:
   * Date: 2020-04-16   * Date: 2020-04-16
   * Author: Pedro Magalhães, pmmaga@php.net   * Author: Pedro Magalhães, pmmaga@php.net
-  * Status: Draft+  * Status: Implemented 
 +  * Target: 8.0
   * First Published at: http://wiki.php.net/rfc/inheritance_private_methods   * First Published at: http://wiki.php.net/rfc/inheritance_private_methods
  
 ===== Introduction =====  ===== Introduction ===== 
  
-Currently, a method with the same name as a parent's method, is checked for inheritance rules regardless of the parent's method visibility.+Currently, a method with the same name as a parent's method, is still checked for some inheritance rules regardless of the parent's method visibility.
  
 This leads to inheritance checks being executed even if the parent's method is private. Since private methods are not callable outside of the scope they are defined in, these rules should not be applied. This leads to inheritance checks being executed even if the parent's method is private. Since private methods are not callable outside of the scope they are defined in, these rules should not be applied.
 +
 +Some rules are already skipped like the number of arguments or their type, but other rules are still enforced. Namely:
 +  * When a method has the same name as a parent's final private method
 +  * When a method has the same name as a parent's static private method and the child's method is non-static, or vice-versa
 +  * When a method has the same name as a parent's concrete private method and the child's method is abstract
  
 The [[http://php.net/manual/en/language.oop5.inheritance.php|documentation]] explicitly states that only public and protected methods are inherited: The [[http://php.net/manual/en/language.oop5.inheritance.php|documentation]] explicitly states that only public and protected methods are inherited:
Line 18: Line 24:
 ===== Proposal =====  ===== Proposal ===== 
  
-This RFC aims at ignoring these inheritance rules in the case of the parent method being private.+This RFC proposes removing these inappropriate inheritance checks that are still enforced in the case of the parent method being private.
  
 In the current state: In the current state:
  
-<code php>  +<code php>
 <?php <?php
    
Line 42: Line 48:
 Produces: Produces:
  
-<code> +<code>
 Fatal error: Cannot override final method A::finalPrivate() Fatal error: Cannot override final method A::finalPrivate()
 </code> </code>
  
-Besides the error ocurring, the message mentions overriding which isn't applicable for private methods.+Besides the error occurring, the message mentions overriding which isn't applicable for private methods.
  
 With the implementation of this RFC, the previous and the following code would compile correctly: With the implementation of this RFC, the previous and the following code would compile correctly:
Line 89: Line 95:
  
 <code> <code>
 +Warning: Private methods cannot be final as they are never overridden by other classes in ...
 A::myPrivate A::myPrivate
 A::myPrivate A::myPrivate
Line 95: Line 102:
 </code> </code>
  
-The final keyword when applied to a private method should have no significance.+The final keyword when applied to a private method should have no significance. In most cases, a correct approach is to use ''final protected'' modifiers in a function where overriding is not desired.
  
-Open issueApplying final to private method could issue compiler warning to warn the user that it doesn'have an effect.+**NOTE:** Due to how common the usage of ''<nowiki>final private function __construct</nowiki>'' is and given that the same results cannot be achieved with ''protected'' visibility, an exception to this rule is made for constructors. With this exception, they are the only case where a child class can'override a ''final private'' method.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
  
-There are no BC breaking changes.+The new compiler warning:
  
-NOTEThe construction ''final private function'' is used for instance to restrict the usage of constructors and cloning of child classes. +<code> 
-A correct approach to attain a similar effect would be ''final protected function''.+WarningPrivate methods cannot be final as they are never overridden by other classes in ..
 +</code>
  
-If the optional compiler warning is implemented this change would cause some existing applications to start issuing that warning.+Will start appearing in some code bases where both modifiers ''final'' and ''private'' are used. However, given that an exception is made for the most common case ''final private function __construct'', I believe the warning is useful to let users know about this change.
  
 ===== Proposed PHP Version(s) =====  ===== Proposed PHP Version(s) ===== 
  
 PHP 8.0 PHP 8.0
- 
-===== Open Issues ===== 
- 
-  * Producing a compiler warning when ''final private function'' is used. 
  
 ===== Unaffected PHP Functionality ===== ===== Unaffected PHP Functionality =====
Line 120: Line 124:
 The inheritance rules for visible methods remains unaffected, including when these are static or declared on a trait. The inheritance rules for visible methods remains unaffected, including when these are static or declared on a trait.
  
-===== Proposed Voting Choices =====+===== Vote ===== 
 + 
 +Voting started 2020-06-15 and ends 2020-06-29.
  
-2/3 majority+<doodle title="Remove inappropriate inheritance signature checks on private methods" auth="pmmaga" voteType="single" closed="true"> 
 +   * Yes 
 +   * No 
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
rfc/inheritance_private_methods.1587067038.txt.gz · Last modified: 2020/04/16 19:57 by pmmaga