rfc:class_const_visibility

This is an old revision of the document!


PHP RFC: Support Class Constant 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.

  • Defining bitmasks/magic numbers, but 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.

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.

Proposed syntax:

<?php
 
class Token {
	// Constants default to public
	const PUBLIC_CONST = 0;
 
        // Constants then also can have a defined visibility
        private const PRIVATE_CONST = 0;
        protected const PROTECTED_CONST = 0;
        public const PUBLIC_CONST_TWO = 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);
}

Backward Incompatible Changes

Reflection Ext

The reflection extension has been updated to expose constants like http://php.net/manual/en/class.reflectionproperty.php.

Instead of getConstant/getConstants returning values there is now a dedicated ReflectionClassConstant class.

<?php
 
class testClass  {
  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();

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 now non-existant class_constants HashTable) there will be breakage

To Opcache

Need update.

Vote

Simple Yes/No option. This requires a 2/3 majority. This vote will close on 06:00 UTC on Tuesday 2015-10-27

Class Constant Visibility
Real name Yes No
ajf (ajf)  
bishop (bishop)  
bwoebi (bwoebi)  
datibbaw (datibbaw)  
davey (davey)  
derick (derick)  
galvao (galvao)  
googleguy (googleguy)  
gooh (gooh)  
guilhermeblanco (guilhermeblanco)  
hywan (hywan)  
ircmaxell (ircmaxell)  
jpauli (jpauli)  
krakjoe (krakjoe)  
leigh (leigh)  
marcio (marcio)  
mbeccati (mbeccati)  
ocramius (ocramius)  
pasindu (pasindu)  
peehaa (peehaa)  
rasmus (rasmus)  
rdlowrey (rdlowrey)  
salathe (salathe)  
santiagolizardo (santiagolizardo)  
stas (stas)  
subjective (subjective)  
toby (toby)  
tpunt (tpunt)  
trowski (trowski)  
zimt (zimt)  
Count: 28 2

Patches and Tests

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

References

Changelog

rfc/class_const_visibility.1445362469.txt.gz · Last modified: 2017/09/22 13:28 (external edit)