72

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?

Marco Demaio
  • 33,578
  • 33
  • 128
  • 159
leo
  • 2,022
  • 2
  • 17
  • 18
  • 11
    With Reflection you can retrieve anything. So there wouldn't be any way to really hide the data even if there were private constants. – zerkms Jul 21 '11 at 01:33
  • 3
    @zerkms It might be useful to have private constants when generating doxygen documentation (that is, when you're telling doxygen not to expose private class members). – Marcello Romani Oct 10 '13 at 07:27
  • 1
    @zerkms what do you mean with "Reflection"? – My1 Feb 23 '17 at 12:06
  • 2
    @My1 http://php.net/manual/en/book.reflection.php – zerkms Feb 23 '17 at 19:11
  • 1
    @zerkms lol. but technically you dont even need to go as extreme as that. if the script is already on the server and it can do file_get_contents as the script user, and you can open any constant or pseudo constant (like priv static vars with setting directly in the file) – My1 Feb 24 '17 at 00:05
  • 1
    @My1 parsing code might be tricky. And you also need to know the path to the file, but counted as a possibility yep :-) – zerkms Feb 24 '17 at 00:06
  • but why is something as screwed up and dangerous like this in PHP, I mean this completely and utterly shatters the point of private structures. like I extra made a whole init dance for giving the private vars of my static config class to my static auth class (I dont like OOP but I ike static classes coz they can be like a toolkit, giving me what I need without the stupid details. – My1 Feb 24 '17 at 00:14

2 Answers2

73

As of PHP 7.1, there are real private constants.

private const PRIVATE_CONST = 0;

See the Class Constant Visibility RFC for more information.

Jeroen De Dauw
  • 10,321
  • 15
  • 56
  • 79
  • 3
    Traits are not in the discussion scope just wanna add that is not allowed on a trait... – obinoob Apr 15 '17 at 13:16
  • "[Traits](https://stackoverflow.com/questions/9205083/traits-vs-interfaces/13648732#13648732) are not in the scope of this Stack Overflow question. I just want to add that that is not allowed on a trait..." – Peter Mortensen Jul 14 '19 at 11:17
  • Correct me if I'm wrong. But in my opinion the visibility of a class constant is only important when it comes to inheritance. A constant can only be defined once and given a value. So it doesn't matter if the variable is visible for the rest of the code or not, because the rest of the code has read-only access anyway. Only if I want to derived from this class then I have to think if the const should be part of the derived class. If so the const should be public or protected. If it is private the only way to access this const is by using the parent keyword. – Alexander Behling Oct 06 '21 at 11:02
58

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sudhir chauhan
  • 862
  • 7
  • 3