为什么 PHP 不允许私有 const? [英] Why doesn't PHP permit private const?

查看:51
本文介绍了为什么 PHP 不允许私有 const?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类受益于在其内部实现中使用常量,但我想限制这些常量的可见性.为什么 PHP 不允许私有常量?是否有其他方法可以实现这一点,或者 PHP 是否试图阻止我不知道的某种设计失误?

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?

推荐答案

使用 private static 属性.

在这种情况下,您将在所有对象中拥有相同的变量,如果您想将其范围扩展到嵌套,您可以公开一个 getter 方法来获取其值并限制变量设置.

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.

这篇关于为什么 PHP 不允许私有 const?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆