# RFC: Class constant visibility modifiers

- Status: Implemented (PHP 7.1)
- Authors: Sean DuBois sean@siobud.com, Reeze Xia reeze@php.net
- Date: 2015-09-13
- Version: 0.2
- Original: https://wiki.php.net/rfc/class_const_visibility
- Page: https://externals.io/rfc/class_const_visibility
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### Class Constant Visibility (closed)

Yes: 28 · No: 2

Voters: ajf (Yes), bishop (Yes), bwoebi (Yes), datibbaw (Yes), davey (Yes), derick (Yes), galvao (Yes), googleguy (Yes), gooh (Yes), guilhermeblanco (Yes), hywan (Yes), ircmaxell (Yes), jpauli (Yes), krakjoe (Yes), leigh (Yes), marcio (Yes), mbeccati (Yes), ocramius (Yes), pasindu (Yes), peehaa (Yes), rasmus (Yes), rdlowrey (Yes), salathe (Yes), santiagolizardo (Yes), stas (No), subjective (Yes), toby (Yes), tpunt (Yes), trowski (Yes), zimt (No)

## Discussions on the mailing list

- [RFC Karma for Sean-Der](https://externals.io/message/88056.md) — started on 2015-09-05, 3 messages
- [\[RFC\] \[DISCUSSION\]: Class Constant Visibility](https://externals.io/message/88700.md) — started on 2015-10-06, 1 messages
- [\[RFC\] \[VOTE\] Class Constant Visibility](https://externals.io/message/88893.md) — started on 2015-10-20, 16 messages
- [Karma to vote on (my own) RFC and access to phpng-upgrading](https://externals.io/message/88894.md) — started on 2015-10-20, 1 messages

---

## 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](http://stackoverflow.com/questions/5339456/php-class-constant-visibility)

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 explicit visibility keyword are defined as public.

Proposed syntax:

```php
<?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;
 
        //Constants can only have one visibility declaration list
        private const FOO = 1, BAR = 2;
}
 
 
//Interfaces only support public consts, and a compile time error will be thrown for anything else. Mirroring the behavior of methods.
interface ICache {
        public const PUBLIC = 0;
        const IMPLICIT_PUBLIC = 1;
}
 
//Reflection was enhanced to allow fetching more than just the values of constants
class testClass  {
  const TEST_CONST = 'test';
}
 
$obj = new ReflectionClass( "testClass" );
$const = $obj->getReflectionConstant( "TEST_CONST" );
$consts = $obj->getReflectionConstants();
```

## Backward Incompatible Changes

## 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-existent 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

**Vote: Class Constant Visibility** — Yes: 28 · No: 2 (closed)

## 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>

This feature was merged into PHP master here: <https://github.com/php/php-src/commit/a75c195000b3226904103244fa9c3d0ce1111838>

## 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>
- V0.2 Implemented
