rfc:class_const_visibility

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
Last revisionBoth sides next revision
rfc:class_const_visibility [2015/03/04 04:07] reezerfc:class_const_visibility [2016/02/20 02:26] reeze
Line 1: Line 1:
 ====== PHP RFC: Support Class Constant Visibility ====== ====== PHP RFC: Support Class Constant Visibility ======
-  * Version: 0.1 +  * Version: 0.2 
-  * Date: 2015-03-03 +  * Date: 2015-09-13 
-  * Author: Reeze Xiareeze@php.net +  * Author: Sean DuBois <sean@siobud.com>, Reeze Xia <reeze@php.net> 
-  * Status: Draft+  * Status: Implemented
   * First Published at: http://wiki.php.net/rfc/class_const_visibility   * First Published at: http://wiki.php.net/rfc/class_const_visibility
  
Line 9: Line 9:
 ===== Introduction ===== ===== Introduction =====
  
-Currently class method and member have visibility control, but class contant is not. +Classes in PHP allow modifiers on properties and methods, but not constants
-They always public. But in real applicationone might want to limit class constant'+It is an easily fixed inconsistencyand a feature that many want and most surprised 
-visibility for better encapsulation. +that it doesn't already exist [[http://stackoverflow.com/questions/5339456/php-class-constant-visibility|Stack Overflow Thread]]
- +
-This RFC intend to introduce class constant's vibility.+
  
 +In a thread on php-internals a couple real world examples were offered.
 +    * Defining bitmasks/magic numbers, but not exposing them globally. Before constants would be exposed allowing callers to depend on them.
 +    * Help make it more clear what is important, exposing harmless constants clutters documentation  needlessly. 
  
 ===== Proposal ===== ===== Proposal =====
  
-This RFC propose PHP support class constant visibilty. The syntax is straightfoward as +This RFC propose PHP support class constant visibility that mirror the behavior of method 
-other member visibility modifiers.+and property visibility. 
  
 Class constant may be define as public, private or protected. class constants declared Class constant may be define as public, private or protected. class constants declared
-without any explict visibility keyword are defined as public.+without any explicit visibility keyword are defined as public. 
  
 Proposed syntax: Proposed syntax:
Line 30: Line 31:
  
 class Token { class Token {
- // Declear public class constants + // Constants default to public 
- const T_ISSET = 0; + const PUBLIC_CONST = 0; 
- const T_ECHO = 1;+ 
 +        // Constants then also can have a defined visibility 
 +        private const PRIVATE_CONST = 0; 
 +        protected const PROTECTED_CONST = 0; 
 +        public const PUBLIC_CONST_TWO = 0; 
 +         
 +        //Constants can only have one visibility declaration list 
 +        private const FOO = 1, BAR = 2;
 } }
  
-echo Token::T_ISSET; // accessiable 
- 
- 
-// no special limit, the same as class 
-abstract class AbstractClass { 
- 
-} 
  
 +//Interfaces only support public consts, and a compile time error will be thrown for anything else. Mirroring the behavior of methods.
 interface ICache { interface ICache {
- private const CANNOT_BE_PRIVATE 1    // error interface can only have pulic class const +        public const PUBLIC 0
- protected const CANNOT_BE_PROTECTED 2; +        const IMPLICIT_PUBLIC 1;
- +
- public function get($k, $v); +
- public function set($k);+
 } }
  
-class CacheStorage extends ICache +//Reflection was enhanced to allow fetching more than just the values of constants 
- protected const CACHE_KEY_PREFIX = ""; +class testClass  
- +  const TEST_CONST 'test';
- public const STORAGE_NAME "Unknow";+
 } }
  
-class RedisCacheStorage extends CacheStorage { +$obj new ReflectionClass( "testClass)
- private const DEFAULT_HOST = "127.0.0.1"; +$const = $obj->getReflectionConstant( "TEST_CONST)
- private const DEFAULT_PORT = "8129"; +$consts $obj->getReflectionConstants();
- protected const CACHE_KEY_PREFIX "my_redis_cache_prefix_";+
  
- public const STORAGE_NAME = "redis";+</code>
  
- private $con; 
- public function __construct() { 
- echo parent:: CACHE_KEY_PREFIX; // access parent; 
- 
- $this->con = redis_fake_always_success_connect(self::DEFAULT_HOST, self::DEFAULT_PORT); 
- } 
- 
- public function set($k, $v) { 
- return redis_fake_set($this, self::CACHE_KEY_PREFIX . $k, $v); 
- } 
- 
- public function get($k) { 
- return redis_fake_set($this, self::CACHE_KEY_PREFIX . $k, $v); 
- } 
-} 
- 
-echo RedisCacheStorage::DEFAULT_HOST; // cloudn't access 
-echo RedisCacheStorage::CACHE_KEY_PREFIX; // cloudn't access 
-echo RedisCacheStorage::STORAGE_NAME; // Yes we can 
- 
-</code> 
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
- 
-No BC break. 
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
  
-PHP7+This RFC targets PHP 7.1
  
 ===== RFC Impact ===== ===== RFC Impact =====
 +
 ==== To SAPIs ==== ==== To SAPIs ====
-No+ 
 +None.
  
 ==== To Existing Extensions ==== ==== To Existing Extensions ====
  
-Maybe? Need to check.+None, all the class constant APIs will be the same. 
 +There will be new _ex APIs that allow callers to explicitly pass flags. 
 + 
 +If a extension accesses non-public structures (the now non-existent class_constants HashTable) there will be breakage
  
 ==== To Opcache ==== ==== To Opcache ====
  
-Need update. Will try to do it later.+Need update.
  
-==== New Constants ====+===== Vote =====
  
-None+Simple Yes/No option. This requires a 2/3  majority.
  
-==== php.ini Defaults ====+This vote will close on 06:00 UTC on Tuesday 2015-10-27
  
-None +<doodle title="Class Constant Visibility" auth="sdubois" voteType="single" closed="true"> 
- +   * Yes 
-===== Open Issues ===== +   * No 
- +</doodle>
- +
-===== Unaffected PHP Functionality ===== +
- +
- +
-===== Future Scope ===== +
- +
-Support final modifier? Need another RFC upon this. +
- +
- +
-===== Proposed Voting Choices ===== +
- +
-Simple Yes/No option. This requires a 2/3  majority.+
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
  
-Work In ProgressTODO+A pull request has been madeIt is feature complete but needs review, more tests, and help with opcache changes : https://github.com/php/php-src/pull/1494
  
-Make it clear if the patch is intended to be the final patch, or is just a prototype. +This feature was merged into PHP master here: https://github.com/php/php-src/commit/a75c195000b3226904103244fa9c3d0ce1111838
- +
-===== Implementation ===== +
- +
-TBD+
  
 ===== References ===== ===== References =====
-http://php.net/manual/en/language.oop5.constants.php +  * http://php.net/manual/en/language.oop5.constants.php 
-http://php.net/manual/en/language.oop5.visibility.php +  http://php.net/manual/en/language.oop5.visibility.php
- +
-===== Rejected Features ===== +
- +
-None+
  
 ===== Changelog ===== ===== Changelog =====
  
-V0.1 Initial version+  * V0.1 Initial version 
 +  * V0.2 Adopted by Sean DuBois <sean@siobud.com> 
 +  * V0.2 Implemented
rfc/class_const_visibility.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1