Setting Visibility For Your Class Constant’s In PHP

Now in PHP 7.1+, you can set different visibility modifiers for each of your class constants. The available visibility modifiers consist of public, protected, and private. Any constant’s within your class set to protected or private that you try to access directly outside of the class will throw a Fatal Error.

Example Usage


class FooBarBaz
    public const FOO = 'Test Public Value';
    protected const BAR = 'Test Protected Value';
    private const BAZ = 'Test Private Value';

echo FooBarBaz::FOO; // Works Perfectly.
echo FooBarBaz::BAR; // Throws a Fatal Error.
echo FooBarBaz::BAZ; // Throws a Fatal Error.