PHP Class Constants - Public, Private or Protected?

PhpConstants

Php Problem Overview


Am I correct in assuming that const properties are automatically public? Is there a way to make them private or protected?

Thanks in advance.

Php Solutions


Solution 1 - Php

Historically, class constants were always publicly accessible as long as the class was loaded and there was no way to change this.

As of PHP 7.1, they remain public by default but access modifiers may now be applied. Here's the example from the release notes:

<?php
class ConstDemo
{
    const PUBLIC_CONST_A = 1;
    public const PUBLIC_CONST_B = 2;
    protected const PROTECTED_CONST = 3;
    private const PRIVATE_CONST = 4;
}

Solution 2 - Php

Class constants should have the option of being private/protected because being public exposes internal details of the class that other classes/code can mistakingly use thinking they are ok to use because they are public.

It would be nice to know that changing a private constant would ONLY affect the class it's defined in. Unfortunately we don't have that option.

Remember back to when you were learning Object Design & Analysis... you give class methods and attributes the most RESTRICTIVE access possible, and later relax them as needed (much harder to go back the other way because other classes/code start using them which would then break other code).

WORKAROUND

Best bet is to just create a private or protected variable and upper-case it to show it's a constant. You could always create a class called constant($value_to_be_constant) that implements the correct magic methods / spl interfaces to prevent it from being changed.

Solution 3 - Php

I am aware this question is 6 years old

Php 7.1 (currently RC1) allows to specify visibility on class constants.

class Token {
        // Constants default to public
        const PUBLIC_CONST = 0;
 
        // Constants then also can have a defined visibility
        private const PRIVATE_CONST = 0;
        protected const PROTECTED_CONST = 0;
        public const PUBLIC_CONST_TWO = 0;
 
        //Constants can only have one visibility declaration list
        private const FOO = 1, BAR = 2;
}

Additional info

Solution 4 - Php

As of php7.1, you can define your class constants with access modifiers (public, private or protected). Have a look at the following example:

<?php
class superheroes{
	public const kal_el = 'Superman';
	protected const bruce_wayne = 'Batman'; # works php7.1 onwards
	private const anthony_stark = 'Iron Man'; # works php7.1 onwards

	public static function show_remaining(){
		echo self::bruce_wayne, '<br />';
		echo self::anthony_stark, '<br />';
	}
}
echo superheroes::kal_el, '<br />';
superheroes::show_remaining();

Credits: http://dwellupper.io/post/48/defining-class-constants-in-php

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser154759View Question on Stackoverflow
Solution 1 - PhpBoltClockView Answer on Stackoverflow
Solution 2 - PhpScottView Answer on Stackoverflow
Solution 3 - PhpmrokView Answer on Stackoverflow
Solution 4 - PhpPranav RanaView Answer on Stackoverflow