Frank Perez

Frank Perez

Keep in touch

An experienced Web Developer with years of experience in design, programming and server administration.

Blog

Overview

© 2024 by Frank Perez.

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

<?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.