rfc:class_const_visibility
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:class_const_visibility [2015/03/04 04:14] – reeze | rfc:class_const_visibility [2015/12/08 16:01] – Merged into 7.1 sean-der | ||
---|---|---|---|
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 Xia, reeze@php.net | + | * Author: |
- | * Status: | + | * Status: |
* First Published at: http:// | * First Published at: http:// | ||
Line 9: | Line 9: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | Currently class method | + | Classes in PHP allow modifiers on properties |
- | They are always be public. | + | It is an easily fixed inconsistency, |
+ | that it doesn' | ||
- | In real application, one might want to limit class constant' | + | In a thread on php-internals a couple |
- | visibility for better encapsulation. | + | * Defining bitmasks/ |
- | + | * Help make it more clear what is important, exposing harmless | |
- | This RFC intend to introduce class constants' visibility. | + | |
===== Proposal ===== | ===== Proposal ===== | ||
- | This RFC propose PHP support class constant | + | This RFC propose PHP support class constant |
- | other member | + | and property |
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 | + | without any explicit |
Proposed syntax: | Proposed syntax: | ||
Line 31: | Line 31: | ||
class Token { | class Token { | ||
- | // Deault: declear | + | // Constants default to public |
- | const | + | const |
- | const T_ECHO = 1; | + | |
- | } | + | |
- | + | ||
- | echo Token:: | + | |
- | // no special limit, the same as normal class | + | |
- | abstract class AbstractClass { | + | |
- | // constant definitions. | + | |
+ | public const PUBLIC_CONST_TWO = 0; | ||
+ | |||
+ | | ||
+ | private const FOO = 1, BAR = 2; | ||
} | } | ||
- | trait Trait1 { | ||
- | // traits are not allowed to define constant as before | ||
- | public const TCONS = 10; // error | ||
- | } | ||
+ | // | ||
interface ICache { | interface ICache { | ||
- | private | + | public |
- | protected | + | const IMPLICIT_PUBLIC |
- | + | ||
- | public function get($k, $v); | + | |
- | public function set($k); | + | |
} | } | ||
- | class CacheStorage extends ICache | + | // |
- | protected const CACHE_KEY_PREFIX = ""; | + | class testClass |
- | + | const TEST_CONST | |
- | public | + | |
} | } | ||
- | class RedisCacheStorage extends CacheStorage { | + | $obj = new ReflectionClass( |
- | private const DEFAULT_HOST | + | $const = $obj->getReflectionConstant( " |
- | private | + | $consts = $obj-> |
- | protected const CACHE_KEY_PREFIX = " | + | |
- | + | ||
- | public const STORAGE_NAME = " | + | |
- | + | ||
- | private $con; | + | |
- | public function __construct() { | + | |
- | echo parent:: CACHE_KEY_PREFIX; | + | |
- | + | ||
- | $this->con = redis_fake_always_success_connect(self:: | + | |
- | } | + | |
- | + | ||
- | public function set($k, $v) { | + | |
- | return redis_fake_set($this, self:: | + | |
- | } | + | |
- | + | ||
- | public function get($k) { | + | |
- | return redis_fake_set($this, | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | echo RedisCacheStorage:: | + | |
- | echo RedisCacheStorage:: | + | |
- | echo RedisCacheStorage:: | + | |
</ | </ | ||
- | At the mean time, internal classes are allowed to define accessiblities as well. | ||
===== 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 | ||
+ | |||
+ | 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" |
- | + | * 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 Progress. TODO | + | A pull request has been made. It is feature complete but needs review, more tests, and help with opcache changes : https:// |
- | 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:// |
- | + | ||
- | ===== Implementation ===== | + | |
- | + | ||
- | TBD | + | |
===== References ===== | ===== References ===== | ||
- | - http:// | + | * http:// |
- | - http:// | + | |
- | + | ||
- | ===== Rejected Features ===== | + | |
- | + | ||
- | None | + | |
===== Changelog ===== | ===== Changelog ===== | ||
- | - V0.1 Initial version | + | * V0.1 Initial version |
+ | * V0.2 Adopted by Sean DuBois < |
rfc/class_const_visibility.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1