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
Next revisionBoth sides next revision
rfc:abstract_final_class [2014/11/27 04:00] – fixed title guilhermeblancorfc:abstract_final_class [2014/11/28 19:36] guilhermeblanco
Line 1: Line 1:
-====== PHP RFC: Abstract final classes ====== +====== PHP RFC: Static classes ====== 
-  * Version: 1.0+  * Version: 1.1
   * Date: 2014-11-26   * Date: 2014-11-26
   * Author: Guilherme Blanco, guilhermeblanco@php.net   * Author: Guilherme Blanco, guilhermeblanco@php.net
Line 8: Line 8:
 ===== Introduction ===== ===== Introduction =====
  
-Abstract final 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.
 Currently, PHP developers' only resource is to create a final class with a private constructor, leading to untestable and error prone code. Currently, PHP developers' only resource is to create a final class with a private constructor, leading to untestable and error prone code.
 +
 +When untestable/error prone code is mentioned is that there's no signaling of broken definition by creating a regular class with private constructor and other members are not declared as static. Everything will just compile smoothly (file inclusion/autoloading).
 +
 +By the acceptance of this patch, every method declaration would automatically be enforced to behave as static without any further keyword needed. This reduces the human error of methods being forgotten to be declared as static.
  
 For such, here is motivation: For such, here is motivation:
  
-  * As "abstract", it cannot be instantiated +  * As "static", it purely behaves at class level, not at instance level 
-  * As "final", it cannot be extended (such as visibility increase, 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 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.
  
 ===== Proposal ===== ===== Proposal =====
  
-Change language scanner to accept abstract final class constructor, and subsequently restricting to only static members.+Change language scanner to accept static class construction, and subsequently auto-infering static members.
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 27: Line 31:
 ===== Example ===== ===== Example =====
  
-  abstract final class StringUtil+  final static class Environment
   {   {
-      public static function isNotNullOrSpace($str)+      private static $rootDirectory = '/var/www/project'; 
 +      public function getRootDirectory()
       {       {
-          return !($str === null || strlen($str) < 1 || strlen(trim($str)) < 1);+          return self::$rootDirectory;
       }       }
   }   }
  
-  echo StringUtil::isNotNullOrSpace('') +  echo Environment::getRootDirectory();
-      ? 'Empty string' +
-      : 'Non-empty string';+
  
 ===== Proposed Voting Choices ===== ===== Proposed Voting Choices =====
  
-Good question... 2/3? or 50% + 1?+Since this is a language update, it requires 2/3 of acceptance
  
 ===== Implementation ===== ===== Implementation =====
  
-https://github.com/php/php-src/pull/923+To be updated. Originally suggested as abstract class at https://github.com/php/php-src/pull/923
rfc/abstract_final_class.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1