PHP 类常量——公共的、私有的还是受保护的?
假设 const 属性自动公开,我是否正确?有没有办法将它们设为私有或受保护?
Am I correct in assuming that const properties are automatically public? Is there a way to make them private or protected?
提前致谢.
推荐答案
从历史上看,只要类被加载并且无法更改,类常量总是可以公开访问的.
Historically, class constants were always publicly accessible as long as the class was loaded and there was no way to change this.
从 PHP 7.1 开始,它们默认保持公开,但 现在可以应用访问修饰符.以下是发行说明中的示例:
As of PHP 7.1, they remain public by default but access modifiers may now be applied. Here's the example from the release notes:
<?php
class ConstDemo
{
const PUBLIC_CONST_A = 1;
public const PUBLIC_CONST_B = 2;
protected const PROTECTED_CONST = 3;
private const PRIVATE_CONST = 4;
}
相关文章