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

查看:108
本文介绍了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天全站免登陆