Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Class Constants - Public, Private or Protected?

Tags:

php

constants

People also ask

Should class constants be public or private?

If you have constants that are only valid to a particular class or one of its subclasses, declare them as either protected or public and place them on the top class in the hierarchy.

Are class constants public?

Class Constants ¶ It is possible to define constants on a per-class basis remaining the same and unchangeable. The default visibility of class constants is public .

Can constants be private?

Answer 5518b447937676d350004c42. Constants can be used with public or private just fine!

Are PHP constants global?

PHP constants are said to have global scope. This basically means that once you have defined a constant it is accessible from any function or object in your script. In addition, PHP provides a number of built-in constants that are available for use to make life easier for the PHP developer.


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;
}

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.


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

  • RFC on class const visibility
  • Blog post on Class Constant Visibility

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