Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't PHP permit private const?

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?

like image 300
leo Avatar asked Jul 21 '11 01:07

leo


2 Answers

As of PHP 7.1, there are real private constants.

private const PRIVATE_CONST = 0;

See the Class Constant Visibility RFC for more information.

like image 51
Jeroen De Dauw Avatar answered Oct 18 '22 19:10

Jeroen De Dauw


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.

like image 36
sudhir chauhan Avatar answered Oct 18 '22 19:10

sudhir chauhan