Why doesn't PHP permit private const?

PhpOopConstantsPrivateEncapsulation

Php Problem Overview


I have a class that benefits from the use of constants in its internal implementation, but I would like to limit visibility of these constants. Why doesn't PHP permit private constants? Is there another way to achieve this or is PHP trying to discourage some type of design misstep I am ignorant of?

Php Solutions


Solution 1 - Php

As of PHP 7.1, there are real private constants.

private const PRIVATE_CONST = 0;

See the Class Constant Visibility RFC for more information.

Solution 2 - Php

Use private static properties.

In that case you will have the same variable throughout all objects and if you want to extend its scope to nested, you can expose a getter method to get its value and restrict variables settings.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionleoView Question on Stackoverflow
Solution 1 - PhpJeroen De DauwView Answer on Stackoverflow
Solution 2 - Phpsudhir chauhanView Answer on Stackoverflow