This is an old revision of the document!
PHP RFC: Support Class Constant Visibility
- Version: 0.2
- Date: 2015-09-13
- Author: Sean DuBois sean@siobud.com, Reeze Xia reeze@php.net
- Status: Draft
- First Published at: http://wiki.php.net/rfc/class_const_visibility
Introduction
Classes in PHP allow modifiers on properties and methods, but not constants. It is an easily fixed inconsistency, and a feature that many want and most surprised that it doesn't already exist. Stack Overflow Thread
In a thread on php-internals a couple real world examples were offered.
- Keeping constants in a parent class like a Database Username/Password
- Not allowing child classes to accidentally mutate a constant in a parent.
Proposal
This RFC propose PHP support class constant visibility that mirror the behavior of method and property visibility.
Class constant may be define as public, private or protected. class constants declared without any explict visibility keyword are defined as public. Class constants may also have the final flag, that prevents inherited constants value from being changed.
Proposed syntax:
<?php class Token { // Constants default to public const PUBLIC_CONST = 0; // Constants then also can have a defined visibility private PRIVATE_CONST = 0; protected PROTECTED_CONST = 0; public PUBLIC_CONST_TWO = 0; // Constants may also have the final modifier, for constants that are inherited but may not be overridden -- TBD final private FINAL_CONST = 0; final FINAL_IMPLICIT_PUBLIC_CONST = 0; } //Interfaces only support protected/public const, and a compile time error will be throw for privates interface ICache { public const PUBLIC = 0; protected const PROTECTED = 0; public function get($k, $v); public function set($k); }
Final Modifier
This RFC currently includes the final modifier, but may be put to a separate vote if it raises concerns.
Backward Incompatible Changes
No BC break.
Proposed PHP Version(s)
This RFC targets PHP 7.1
RFC Impact
To SAPIs
None.
To Existing Extensions
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 class_constants HashTable) there will be breakage
To Opcache
Need update.
Proposed Voting Choices
Simple Yes/No option. This requires a 2/3 majority.
Patches and Tests
A pull request has been made, but not complete. It needs review, and help with opcache changes : https://github.com/php/php-src/pull/1494
References
- http://php.net/manual/en/language.oop5.constants.php - http://php.net/manual/en/language.oop5.visibility.php
Changelog
- V0.1 Initial version - V0.2 Adopted by Sean DuBois sean@siobud.com