rfc:abstract_final_class

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:abstract_final_class [2014/11/28 19:35] – Changed from abstract final class to static class guilhermeblancorfc:abstract_final_class [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 3: Line 3:
   * Date: 2014-11-26   * Date: 2014-11-26
   * Author: Guilherme Blanco, guilhermeblanco@php.net   * Author: Guilherme Blanco, guilhermeblanco@php.net
-  * Status: Under Discussion+  * Status: Declined
   * First Published at: http://wiki.php.net/rfc/abstract_final_class   * First Published at: http://wiki.php.net/rfc/abstract_final_class
  
 ===== Introduction ===== ===== Introduction =====
 +
 +Please understand only one of the following proposals can be accepted, not both.
 +They're explained together because the address the same issue.
 +
 +==== As Static classes ====
 +
  
 Static classes are helpful in the case you are wrapping common functions that are static, but the common class itself cannot be instantiated. Static classes are helpful in the case you are wrapping common functions that are static, but the common class itself cannot be instantiated.
Line 20: Line 26:
   * Could also be used with combination of "final", meaning it cannot be extended (such as more permissive visibility, behavior change, etc)   * Could also be used with combination of "final", meaning it cannot be extended (such as more permissive visibility, behavior change, etc)
   * There's no way of adding static variables to a namespace. This would address this issue too.   * There's no way of adding static variables to a namespace. This would address this issue too.
 +
 +==== As abstract final ====
 +
 +
 +
 +Abstract final classes are helpful in the case you are wrapping common functions that are static, but the common class itself cannot be instantiated. Currently, PHP developers' only resource is to create a final class with a private constructor, leading to untestable and error prone code.
 +
 +For such, here is motivation:
 +
 +    As “abstract”, it cannot be instantiated
 +    As “final”, it cannot be extended (such as visibility increase, behavior change, etc)
 +    There's no way of adding variables to a namespace. This would address this issue too
  
 ===== Proposal ===== ===== Proposal =====
  
-Change language scanner to accept static class construction, and subsequently auto-infering static members.+Change language scanner to accept static class or abstract final construction, and subsequently enforcing the existence of only static members.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 31: Line 49:
 ===== Example ===== ===== Example =====
  
-  final static class Environment+==== As static class ==== 
 + 
 +  static class Environment
   {   {
       private static $rootDirectory = '/var/www/project';       private static $rootDirectory = '/var/www/project';
- +      public static function getRootDirectory()
-      public function getRootDirectory()+
       {       {
           return self::$rootDirectory;           return self::$rootDirectory;
Line 43: Line 62:
   echo Environment::getRootDirectory();   echo Environment::getRootDirectory();
  
-===== Proposed Voting Choices =====+==== As abstract final ==== 
 + 
 +  abstract final class StringUtil 
 +  { 
 +      public static function isNotNullOrSpace($str) 
 +      { 
 +          return !($str === null || strlen($str) < 1 || strlen(trim($str)) < 1); 
 +      } 
 +  } 
 + 
 +  echo StringUtil::isNotNullOrSpace(''
 +    ? 'Empty string' 
 +    : 'Non-empty string'; 
  
-Since this is a language updateit requires 2/3 of acceptance+===== Vote ===== 
 +<doodle title="Static classes" auth="guilhermeblanco" voteType="single" closed="true"> 
 +   * Yesas "static class" 
 +   * Yes, as "abstract final class" 
 +   * No 
 +</doodle> 
 +\\ 
 +The vote starts on 12/12/2014 and ends on 12/19/2014. 2/3 majority required.
  
 ===== Implementation ===== ===== Implementation =====
  
-To be updated. Originally suggested as abstract class at https://github.com/php/php-src/pull/923+  * Most recent suggested implementation available at https://github.com/php/php-src/pull/929 
 +  * Originally suggested as abstract class at https://github.com/php/php-src/pull/923
rfc/abstract_final_class.1417203345.txt.gz · Last modified: 2017/09/22 13:28 (external edit)