There are five types of access modifier in PHP: public, private, protected, abstract, and final. Often called visibility modifiers, not all of them are equally applicable. Their use is spread across classes, functions, and variables, as follows:
- Functions: public, private, protected, abstract, and final
- Classes: abstract and final
- Variables: public, private, and protected
Class constants, however, are not on the list. The older versions of PHP did not allow a visibility modifier on the class constant. By default, class constants were merely assigned public visibility.
The PHP 7.1 release addresses this limitation by introducing the public, private, and protected class constant visibility modifiers, as per the following example:
class Visibility
{
// Constants without defined visibility
const THE_DEFAULT_PUBLIC_CONST = 'PHP';
// Constants with defined visibility
private const THE_PRIVATE_CONST = 'PHP';
protected const THE_PROTECTED_CONST = 'PHP';
public const THE_PUBLIC_CONST = 'PHP';
}
Similar to the old behavior, class constants declared without any explicit visibility default to public.