This is an old revision of the document!
PHP RFC: Support Class Constant Visibility
- Version: 0.1
- Date: 2015-03-03
- Author: Reeze Xia, reeze@php.net
- Status: Draft
- First Published at: http://wiki.php.net/rfc/class_const_visibility
Introduction
Currently class method and member have visibility control, but class contants are not. They are always be public.
In real application, one might want to limit class constant's visibility for better encapsulation.
This RFC intend to introduce class constants' visibility.
Proposal
This RFC propose PHP support class constant visibilty. The syntax is straightfoward as other member visibility modifiers.
Class constant may be define as public, private or protected. class constants declared without any explict visibility keyword are defined as public.
Proposed syntax:
<?php class Token { // Deault: declear public class constants as before const T_ISSET = 0; const T_ECHO = 1; } echo Token::T_ISSET; // accessiable // no special limit, the same as normal class abstract class AbstractClass { // constant definitions. } trait Trait1 { // traits are not allowed to define constant as before public const TCONS = 10; // error } interface ICache { private const CANNOT_BE_PRIVATE = 1; // error interface can only have pulic class const protected const CANNOT_BE_PROTECTED = 2; public function get($k, $v); public function set($k); } class CacheStorage extends ICache { protected const CACHE_KEY_PREFIX = ""; public const STORAGE_NAME = "Unknow"; } class RedisCacheStorage extends CacheStorage { 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"; 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
Backward Incompatible Changes
No BC break.
Proposed PHP Version(s)
PHP7
RFC Impact
To SAPIs
No
To Existing Extensions
Maybe? Need to check.
To Opcache
Need update. Will try to do it later.
New Constants
None
php.ini Defaults
None
Open Issues
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
Work In Progress. TODO
Make it clear if the patch is intended to be the final patch, or is just a prototype.
Implementation
TBD
References
- http://php.net/manual/en/language.oop5.constants.php - http://php.net/manual/en/language.oop5.visibility.php
Rejected Features
None
Changelog
- V0.1 Initial version