rfc:interface-default-methods

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:interface-default-methods [2023/06/21 23:47] – Add private methods, parent scoping, and general exposition on behavior and motivation. levimrfc:interface-default-methods [2023/07/17 15:06] (current) – Status: declined levim
Line 3: Line 3:
   * Date: 2022-06-27   * Date: 2022-06-27
   * Author: Levi Morrison, levim@php.net   * Author: Levi Morrison, levim@php.net
-  * Status: Under Discussion+  * Status: Declined
   * First Published at: http://wiki.php.net/rfc/interface-default-methods   * First Published at: http://wiki.php.net/rfc/interface-default-methods
  
Line 44: Line 44:
   - If the above rules fail, then the class needs to implement the method itself, possibly delegating to one of the interfaces.   - If the above rules fail, then the class needs to implement the method itself, possibly delegating to one of the interfaces.
  
-This RFC proposes the same. However, rule 2 has not yet been implemented so I am unsure how feasible it is.+This RFC proposes the same. However, rule 2 is only partially implemented at this time.
  
 Here's an example of a class delegating to another method: Here's an example of a class delegating to another method:
Line 86: Line 86:
  
 The name of the interface should be used instead: The name of the interface should be used instead:
 +
 +<PHP>
 +interface Interface2 extends Interface1 {
 +    function method1() { Interface1::method1(); }
 +}
 +</PHP>
 +
 +However, if a sub-class of the inheriting class calls ''parent::method1()'', it will work:
  
 <PHP> <PHP>
 interface Interface1 { interface Interface1 {
     function method1() { echo __METHOD__, PHP_EOL; }     function method1() { echo __METHOD__, PHP_EOL; }
 +}
 +
 +class Class1 implements Interface1 {
 +    // Inherits Interface1::method1() here.
 +}
 +
 +class Class2 extends Class1 {
 +    function method1() { parent::method1(); }
 +}
 +
 +(new Class2())->method1);
 +// output:
 +// Interface1::method1
 +</PHP>
 +
 +==== Cancelling Default Methods ====
 +If an interface method extends a parent interface method which has a default, this prevents using the default method for classes which implement the child interface but do not directly implement the parent one:
 +
 +<PHP>
 +interface Interface1 {
 +    function method1() { echo __METHOD__, "\n"; }
 } }
  
 interface Interface2 extends Interface1 { interface Interface2 extends Interface1 {
-    function method1() { Interface1::method1(); }+    function method1();
 } }
 +
 +/* Would be an error because method1 has not been implemented.
 +class Class1 implements Interface2 {
 +    // error: method1 has not been implemented.
 +}
 + */
 +
 +// This is subtly different, but valid:
 +class Class1 implements Interface1, Interface2 {}
 </PHP> </PHP>
 +
 +The behavior could go either way. I picked this behavior because if it's wrong, it's easier to correct than the other way around.
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
Line 105: Line 145:
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
-PHP 8.NEXT.+PHP 8.3.
  
 ===== RFC Impact ===== ===== RFC Impact =====
  
-==== To Existing Extensions ==== +==== To Extensions ==== 
-Modules can specify an interface implementation as well.+Modules can specify an interface implementation as well. These internal default methods should not be marked with ''ZEND_ACC_ABSTRACT'' and should be instead marked with ''ZEND_ACC_DEFAULT_METHOD''.
  
 ==== To Opcache ==== ==== To Opcache ====
-Opcache should also work with this feature.+Opcache should also work with this feature. The proof of concept implementation has not triggered any issues so far in CI.
  
 ==== To the Ecosystem ==== ==== To the Ecosystem ====
Line 129: Line 169:
 ===== Voting ===== ===== Voting =====
 The vote will be a simple yes/no vote on whether to include the feature. The vote will be a simple yes/no vote on whether to include the feature.
 +<doodle title="Interface Default Methods" auth="levim" voteType="single" closed="false" closeon="2023-07-17T00:00:00Z">
 +   * Yes
 +   * No
 +</doodle>
 +
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
rfc/interface-default-methods.1687391277.txt.gz · Last modified: 2023/06/21 23:47 by levim