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
rfc:class_const_visibility [2015/10/06 15:15] – Add ReflectionClassConstant API examples sean-derrfc:class_const_visibility [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 3: Line 3:
   * Date: 2015-09-13   * Date: 2015-09-13
   * Author: Sean DuBois <sean@siobud.com>, Reeze Xia <reeze@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 14: Line 14:
  
 In a thread on php-internals a couple real world examples were offered. In a thread on php-internals a couple real world examples were offered.
-    * Keeping constants in a parent class private like a Database Username/Password +    * Defining bitmasks/magic numbers, but not exposing them globally. Before constants would be exposed allowing callers to depend on them. 
-    * Defining bitmasks/magic numbers, but not exposing them globally +    * Help make it more clear what is important, exposing harmless constants clutters documentation  needlessly. 
  
 ===== Proposal ===== ===== Proposal =====
Line 23: Line 23:
  
 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 38: Line 38:
         protected const PROTECTED_CONST = 0;         protected const PROTECTED_CONST = 0;
         public const PUBLIC_CONST_TWO = 0;         public const PUBLIC_CONST_TWO = 0;
 +        
 +        //Constants can only have one visibility declaration list
 +        private const FOO = 1, BAR = 2;
 } }
  
  
-//Interfaces only support protected/public const, and a compile time error will be throw for privates+//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 {
         public const PUBLIC = 0;         public const PUBLIC = 0;
- protected const PROTECTED 0;+        const IMPLICIT_PUBLIC 1; 
 +}
  
- public function get($k, $v); +//Reflection was enhanced to allow fetching more than just the values of constants 
- public function set($k);+class testClass 
 +  const TEST_CONST = 'test';
 } }
 +
 +$obj = new ReflectionClass( "testClass" );
 +$const = $obj->getReflectionConstant( "TEST_CONST" );
 +$consts = $obj->getReflectionConstants();
  
 </code> </code>
Line 54: Line 63:
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
- 
-==== Reflection Ext ==== 
-The reflection extension has been updated to expose constants like [[ReflectionProperty|http://php.net/manual/en/class.reflectionproperty.php]].  
-Instead of just an array of values there is a dedicated ReflectionClassConstant class. 
- 
-<code php> 
-<?php 
- 
-class testClass extends upperClass { 
-  const TEST_CONST = 'test'; 
-} 
- 
-$obj = new ReflectionClass( "testClass" ); 
-$const = $obj->getConstant( "TEST_CONST" ); 
-$const->__toString(); 
-$const->getName(); 
-$const->getValue(); 
-$const->isPublic(); 
-$const->isPrivate(); 
-$const->isProtected(); 
-$const->getModifiers(); 
-$const->getDeclaringClass(); 
-</code> 
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 93: Line 79:
 There will be new _ex APIs that allow callers to explicitly pass flags. There will be new _ex APIs that allow callers to explicitly pass flags.
  
-If a extension accesses non-public structures (the now non-existant class_constants HashTable) there will be breakage+If a extension accesses non-public structures (the now non-existent class_constants HashTable) there will be breakage
  
 ==== To Opcache ==== ==== To Opcache ====
Line 99: Line 85:
 Need update. Need update.
  
-===== Proposed Voting Choices =====+===== Vote =====
  
 Simple Yes/No option. This requires a 2/3  majority. Simple Yes/No option. This requires a 2/3  majority.
 +
 +This vote will close on 06:00 UTC on Tuesday 2015-10-27
 +
 +<doodle title="Class Constant Visibility" auth="sdubois" voteType="single" closed="true">
 +   * Yes
 +   * No
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
Line 107: Line 100:
 A pull request has been made. It is feature complete but needs review, more tests, and help with opcache changes : https://github.com/php/php-src/pull/1494 A pull request has been made. It is feature complete but needs review, more tests, and help with opcache changes : https://github.com/php/php-src/pull/1494
  
 +This feature was merged into PHP master here: https://github.com/php/php-src/commit/a75c195000b3226904103244fa9c3d0ce1111838
  
 ===== References ===== ===== References =====
Line 116: Line 110:
   * V0.1 Initial version   * V0.1 Initial version
   * V0.2 Adopted by Sean DuBois <sean@siobud.com>   * V0.2 Adopted by Sean DuBois <sean@siobud.com>
 +  * V0.2 Implemented
rfc/class_const_visibility.1444144514.txt.gz · Last modified: 2017/09/22 13:28 (external edit)