PHP类常量——公共的、私有的还是受保护的?

时间:2022-11-29 10:55:51

Am I correct in assuming that const properties are automatically public? Is there a way to make them private or protected?

假设const属性是自动公开的,我是对的吗?有没有办法让它们变得私密或受到保护?

Thanks in advance.

提前谢谢。

4 个解决方案

#1


34  

Yes, they are globally accessible so long as the class itself is loaded. As far as I know you can't modify the accessibility of class constants in PHP.

是的,只要类本身被加载,它们是全局可访问的。就我所知,您不能修改PHP中类常量的可访问性。

#2


19  

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.

类常量应该可以选择为private/protected,因为它公开了类的内部细节,其他类/代码可能会错误地认为它们是可以使用的,因为它们是公共的。

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.

最好的方法是创建一个私有的或受保护的变量,用大写字母表示它是一个常数。您可以创建一个名为常量的类($value_to_be_constant),它实现正确的魔术方法/ spl接口,以防止更改。

#3


5  

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

#4


2  

As of php7.1, you can define your class constants with access modifiers (public, private or protected). Have a look at the following example:

从php7.1开始,可以使用访问修饰符(public、private或protected)定义类常量。看看下面的例子:

<?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

#1


34  

Yes, they are globally accessible so long as the class itself is loaded. As far as I know you can't modify the accessibility of class constants in PHP.

是的,只要类本身被加载,它们是全局可访问的。就我所知,您不能修改PHP中类常量的可访问性。

#2


19  

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.

类常量应该可以选择为private/protected,因为它公开了类的内部细节,其他类/代码可能会错误地认为它们是可以使用的,因为它们是公共的。

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.

最好的方法是创建一个私有的或受保护的变量,用大写字母表示它是一个常数。您可以创建一个名为常量的类($value_to_be_constant),它实现正确的魔术方法/ spl接口,以防止更改。

#3


5  

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

#4


2  

As of php7.1, you can define your class constants with access modifiers (public, private or protected). Have a look at the following example:

从php7.1开始,可以使用访问修饰符(public、private或protected)定义类常量。看看下面的例子:

<?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