Articles/PHP/Setting Visibility for Your Class Constants in PHP

Setting Visibility for Your Class Constants 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.

November 13, 2016·1 min read

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

PHP
<?php

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.