28

Can we set visibility of class constant?
For this example:

class MyClass {
    const CONST_VALUE = 'A constant value';
}

Can we specify

public const CONST_VALUE = 'A constant value';

or

private const CONST_VALUE = 'A constant value';

or

protected const CONST_VALUE = 'A constant value';
Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
Poonam Bhatt
  • 10,154
  • 16
  • 53
  • 72
  • 5
    Note that there is an RFC for PHP that proposes class constant visibility, see [PHP RFC: Support Class Constant Visibility](https://wiki.php.net/rfc/class_const_visibility). – Nic Wortel Oct 07 '15 at 17:21
  • No, this language feature is not present (, yet). Its basically the Java feature known as "constants as variables with access levels". You find some more pieces of information on this language feature in the "class const visibility" RFC (see comment above), in this answer: http://stackoverflow.com/a/27762041/1163786 and the Pull Request: https://github.com/php/php-src/pull/1494 – Jens A. Koch Oct 21 '15 at 19:34

7 Answers7

41

Update: visibility modifiers for constants have been added in PHP 7.1 (released 1st of December 2016). See the RFC : Support Class Constant Visibility.

The syntax looks like this:

class ClassName {
    private const PRIVATE_CONST = 0;
    protected const PROTECTED_CONST = 0;
    public const PUBLIC_CONST = 0;
}
Jimbo
  • 25,790
  • 15
  • 86
  • 131
Morgan Touverey Quilling
  • 4,181
  • 4
  • 29
  • 41
  • 2
    OP, consider choosing this as the right answer, because the one from @Alex is no longer true as of PHP 7.1.0 – thexpand Nov 29 '17 at 09:21
21

As of PHP7.1 visibility modifiers are allowed for class constants, in previous versions it's not possible to set the visibility of constants in a class. They're always public. See the comments at http://www.php.net/manual/en/language.oop5.constants.php for more information.

mleko
  • 11,650
  • 6
  • 50
  • 71
Alex
  • 6,228
  • 1
  • 22
  • 18
  • 4
    I found this from comment.. It helped .. "Lest anyone think this is somehow an omission in PHP, there is simply no point to having a protected or private constant. Access specifiers identify who has the right to *change* members, not who has the right to read them" **I do see this as an omission. They are not only access modifiers, but they limit visibility as well. As it is, I can not make a constant that is private to my class, which I see as a problem. I would settle for multiple modifiers like private const $var = 'me'; but that is not allowed either.** – Poonam Bhatt Mar 17 '11 at 13:39
4

An alternative would be to use a Constant Method, e.g.

private static function gravitationalConstant() {
    return 9.81;
}

Quoting from Fowler's Refactoring book:

This idiom is less familiar to C based programmers, but is very familiar to Smalltalkers (who didn't have constants in their language). On the whole I don't tend to use this in Java as it is less idiomatic to the language. However if you need to replace the simple return with a calculated value then it's worth changing the constant field to a constant method. (I guess there should be a refactoring for that....)

Gordon
  • 312,688
  • 75
  • 539
  • 559
3

In PHP Latest version (PHP 7.1.0) it will available.

Sample Syntax was like.

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

Refer below link. https://wiki.php.net/rfc/class_const_visibility

charan
  • 53
  • 4
0

It is now possible in PHP 7.1 released Alpha today which adds Class constant visibility modifiers

brzuchal
  • 649
  • 8
  • 19
0

It is possible in Php 7.1.0. Please visit PHP RFC: Support Class Constant Visibility

Mukesh
  • 7,630
  • 21
  • 105
  • 159
0

Modifiers are not allowed for constants in php. You can use

public static $variable = "abc";

but sadly final is not allowed here.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Jakob Alexander Eichler
  • 2,988
  • 3
  • 33
  • 49