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
Next revisionBoth sides next revision
rfc:class_const_visibility [2015/03/04 04:11] reezerfc:class_const_visibility [2015/12/03 13:56] – Update status ajf
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: Accepted
   * 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 contants are not. +Classes in PHP allow modifiers on properties and methods, but not constants
-They are always be public.+It is an easily fixed inconsistency, and a feature that many want and most surprised 
 +that it doesn't already exist [[http://stackoverflow.com/questions/5339456/php-class-constant-visibility|Stack Overflow Thread]]
  
-In real applicationone might want to limit class constant'+In a thread on php-internals a couple real world examples were offered. 
-visibility for better encapsulation+    * Defining bitmasks/magic numbersbut 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
-This RFC intend to introduce class constants' visibility.+
  
 ===== 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 31: Line 31:
  
 class Token { class Token {
- // Deault: declear public class constants as before + // Constants default to public 
- const T_ISSET = 0; + const PUBLIC_CONST = 0;
- const T_ECHO = 1; +
-}+
  
-echo Token::T_ISSET; // accessiable +        // Constants then also can have a defined visibility 
- +        private const PRIVATE_CONST = 0; 
- +        protected const PROTECTED_CONST = 0; 
-// no special limit, the same as normal class +        public const PUBLIC_CONST_TWO = 0; 
-abstract class AbstractClass { +         
- // constant definitions.+        //Constants can only have one visibility declaration list 
 +        private const FOO = 1, BAR = 2;
 } }
  
-trait Trait1 { 
- // traits are not allowed to define constant as before 
- public const TCONS = 10; // error 
-} 
  
 +//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 { +</code>
- protected const CACHE_KEY_PREFIX = "";+
  
- public const STORAGE_NAME = "Unknow"; 
-} 
  
-class RedisCacheStorage extends CacheStorage { +===== Backward Incompatible Changes =====
- private const DEFAULT_HOST "127.0.0.1"; +
- private const DEFAULT_PORT "8129"; +
- protected const CACHE_KEY_PREFIX "my_redis_cache_prefix_";+
  
- public const STORAGE_NAME "redis";+==== Reflection Ext ==== 
 +The reflection extension has been updated to expose constants like [[ReflectionProperty|http://php.net/manual/en/class.reflectionproperty.php]]. 
  
- private $con; +Instead of getConstant/getConstants returning values there is now a dedicated ReflectionClassConstant class.
- public function __construct() { +
- echo parent:: CACHE_KEY_PREFIX; // access parent;+
  
- $this->con = redis_fake_always_success_connect(self::DEFAULT_HOST, self::DEFAULT_PORT); +<code php
- +<?php
- +
- public function set($k, $v) { +
- return redis_fake_set($this, self::CACHE_KEY_PREFIX . $k, $v); +
- }+
  
- public function get($k) +class testClass  
- return redis_fake_set($this, self::CACHE_KEY_PREFIX . $k, $v); +  const TEST_CONST = 'test';
- }+
 } }
  
-echo RedisCacheStorage::DEFAULT_HOST// cloudn't access +$obj = new ReflectionClass( "testClass" )
-echo RedisCacheStorage::CACHE_KEY_PREFIX// cloudn't access +$const = $obj->getConstant( "TEST_CONST" )
-echo RedisCacheStorage::STORAGE_NAME// Yes we can +$const->__toString()
 +$const->getName(); 
 +$const->getValue(); 
 +$const->isPublic(); 
 +$const->isPrivate(); 
 +$const->isProtected(); 
 +$const->getModifiers(); 
 +$const->getDeclaringClass();
 </code> </code>
- 
-===== 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. 
- 
-===== 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>
rfc/class_const_visibility.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1