php class constant visibility

Poonam Bhatt picture Poonam Bhatt · Mar 17, 2011 · Viewed 11.3k times · Source

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';

Answer

Morgan Touverey Quilling picture Morgan Touverey Quilling · Apr 3, 2016

Update: visibility modifiers for constants have been added in PHP 7.1 (released 1st of December 2016). See the RFC : Support Class Constant Visibility.

The syntax looks like this:

class ClassName {
    private const PRIVATE_CONST = 0;
    protected const PROTECTED_CONST = 0;
    public const PUBLIC_CONST = 0;
}