Search code examples
phpclass-constants

php class constant visibility


Can we set visibility of class constant?
For this example:

class MyClass {
    const CONST_VALUE = 'A constant value';
}

Can we specify

public const CONST_VALUE = 'A constant value';

or

private const CONST_VALUE = 'A constant value';

or

protected const CONST_VALUE = 'A constant value';

Solution

  • As of PHP7.1 visibility modifiers are allowed for class constants, in previous versions it's not possible to set the visibility of constants in a class. They're always public. See the comments at http://www.php.net/manual/en/language.oop5.constants.php for more information.