简体   繁体   English

PHP类常量 - 公共,私有还是受保护?

[英]PHP Class Constants - Public, Private or Protected?

Am I correct in assuming that const properties are automatically public? 假设const属性是自动公开的,我是否正确? Is there a way to make them private or protected? 有没有办法让它们变得私密或受到保护?

Thanks in advance. 提前致谢。

Historically, class constants were always publicly accessible as long as the class was loaded and there was no way to change this. 从历史上看,只要类被加载并且无法改变它,类常量总是可公开访问。

As of PHP 7.1, they remain public by default but access modifiers may now be applied . 从PHP 7.1开始,默认情况下它们仍然是公共的,但现在可以应用访问修饰符 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;
}

Class constants should have the option of being private/protected because being public exposes internal details of the class that other classes/code can mistakingly use thinking they are ok to use because they are public. 类常量应该具有私有/受保护的选项,因为公开暴露了类的内部细节,其他类/代码可能会错误地使用它们认为它们可以使用,因为它们是公共的。

It would be nice to know that changing a private constant would ONLY affect the class it's defined in. Unfortunately we don't have that option. 很高兴知道更改私有常量只会影响它定义的类。不幸的是我们没有那个选项。

Remember back to when you were learning Object Design & Analysis... you give class methods and attributes the most RESTRICTIVE access possible, and later relax them as needed (much harder to go back the other way because other classes/code start using them which would then break other code). 回想一下当你学习对象设计和分析时...你给类方法和属性最可能的限制访问,然后根据需要放松它们(更难以回到另一个方向,因为其他类/代码开始使用它们然后会打破其他代码)。

WORKAROUND 替代方法

Best bet is to just create a private or protected variable and upper-case it to show it's a constant. 最好的办法是创建一个私有或受保护的变量,大写它以显示它是一个常量。 You could always create a class called constant($value_to_be_constant) that implements the correct magic methods / spl interfaces to prevent it from being changed. 您始终可以创建一个名为constant($ value_to_be_constant)的类,该类实现正确的魔术方法/ spl接口以防止其被更改。

I am aware this question is 6 years old 我知道这个问题是6岁

Php 7.1 (currently RC1) allows to specify visibility on class constants. Php 7.1(当前为RC1)允许指定类常量的可见性。

class Token {
        // Constants default to public
        const PUBLIC_CONST = 0;

        // Constants then also can have a defined visibility
        private const PRIVATE_CONST = 0;
        protected const PROTECTED_CONST = 0;
        public const PUBLIC_CONST_TWO = 0;

        //Constants can only have one visibility declaration list
        private const FOO = 1, BAR = 2;
}

Additional info 附加信息

As of php7.1 , you can define your class constants with access modifiers ( public , private or protected ). php7.1开始 ,您可以使用访问修饰符publicprivateprotected )定义类常量。 Have a look at the following example: 看看下面的例子:

<?php
class superheroes{
    public const kal_el = 'Superman';
    protected const bruce_wayne = 'Batman'; # works php7.1 onwards
    private const anthony_stark = 'Iron Man'; # works php7.1 onwards

    public static function show_remaining(){
        echo self::bruce_wayne, '<br />';
        echo self::anthony_stark, '<br />';
    }
}
echo superheroes::kal_el, '<br />';
superheroes::show_remaining();

Credits: http://dwellupper.io/post/48/defining-class-constants-in-php 致谢: http//dwellupper.io/post/48/defining-class-constants-in-php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM