rfc:abstract_trait_method_validation

Differences

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

Link to this comparison view

Next revision
Previous revision
rfc:abstract_trait_method_validation [2020/02/07 10:25]
nikic created
rfc:abstract_trait_method_validation [2020/04/23 10:49] (current)
nikic
Line 2: Line 2:
   * Date: 2020-02-07   * Date: 2020-02-07
   * Author: Nikita Popov <nikic@php.net>   * Author: Nikita Popov <nikic@php.net>
-  * Status: Under Discussion+  * Status: Implemented
   * Target Version: PHP 8.0   * Target Version: PHP 8.0
   * Implementation: https://github.com/php/php-src/pull/5068   * Implementation: https://github.com/php/php-src/pull/5068
Line 27: Line 27:
     private function neededByTheTrait(): string { }     private function neededByTheTrait(): string { }
  
-    // This is also allowed (relaxing visibility+    // This is forbidden (incorrect return type
-    public function neededByTheTrait(): string { }+    private function neededByTheTrait(): stdClass { }
          
     // This is forbidden (non-static changed to static)     // This is forbidden (non-static changed to static)
     private static function neededByTheTrait(): string { }     private static function neededByTheTrait(): string { }
- 
-    // This is forbidden (incorrect return type) 
-    private function neededByTheTrait(): stdClass { } 
 } }
 </PHP> </PHP>
Line 117: Line 114:
 This RFC proposes to always validate the signature of abstract trait methods against the implementing method, independently of its origin. Additionally the incorrect bidirectional cross-trait compatibilty check from the last example is removed. This RFC proposes to always validate the signature of abstract trait methods against the implementing method, independently of its origin. Additionally the incorrect bidirectional cross-trait compatibilty check from the last example is removed.
  
-Verification for abstract trait methods follows the same rules as for other methods and results in a fatal error if the methods are not compatible:+fatal error is generated if the implementing method is not compatible with the abstract trait method, where compatibility entails:
  
   * The signature must be compatible, which includes arity compatibility, contravariant parameter type compatibility and covariant return type compatibility.   * The signature must be compatible, which includes arity compatibility, contravariant parameter type compatibility and covariant return type compatibility.
-  * The implementing method must have the same visibility as the abstract method, or be more visible. (In particular this means that a private abstract method allows an implementation of any visibility.) +  * The static-ness of the methods must match.
-  * The static-ness of the method must match.+
  
 Additionally, this RFC allows the declaration of abstract private methods in traits only. Normally abstract private methods are a contradiction in terms, because the method providing the implementation would not be visible from the class issuing the requirement. However, abstract private methods are well-defined inside traits, because trait methods have access to private methods of the using class. Additionally, this RFC allows the declaration of abstract private methods in traits only. Normally abstract private methods are a contradiction in terms, because the method providing the implementation would not be visible from the class issuing the requirement. However, abstract private methods are well-defined inside traits, because trait methods have access to private methods of the using class.
  
 Private abstract methods must be implemented by the using class. Their implementation cannot be postponed by marking the class abstract, as this would once again render the implementation inaccessible. Private abstract methods must be implemented by the using class. Their implementation cannot be postponed by marking the class abstract, as this would once again render the implementation inaccessible.
 +
 +Contrary to the usual inheritance rules, the visibility level of the abstract trait method is **not** enforced. This means that an ''abstract protected'' method in the trait can be implemented by a ''private'' method in the class, even though this reduces visibility. This exception is added for backwards-compatibility reasons: Because abstract private methods were forbidden prior to this proposal, a private requirement for the trait could not be specified with private visibility. This exception allows code to use ''abstract protected'' trait methods with private implementations and thus be compatible with both PHP 7 and PHP 8. Code that only needs to be compatible with PHP 8 should prefer the use of ''abstract private'' methods for clarity.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 133: Line 131:
 ===== Vote ===== ===== Vote =====
  
-Yes/No.+Voting opened 2020-03-06 and closes 2020-03-20. 
 + 
 +<doodle title="Validate abstract trait methods?" auth="nikic" voteType="single" closed="true"> 
 +   Yes 
 +   No 
 +</doodle>
  
rfc/abstract_trait_method_validation.1581071107.txt.gz · Last modified: 2020/02/07 10:25 by nikic