Why doesn#39;t PHP permit private const?(为什么 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 PHP 不允许私有 const?


基础教程推荐
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01