Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

Can we set visibility of class constant? For ex

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

for this class canwe write

public const CONST_VALUE = 'A constant value';

or

   private const CONST_VALUE = 'A constant value';

or

   protected const CONST_VALUE = 'A constant value';
share|improve this question
5  
Note that there is an RFC for PHP that proposes class constant visibility, see PHP RFC: Support Class Constant Visibility. – Nic Oct 7 '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: stackoverflow.com/a/27762041/1163786 and the Pull Request: github.com/php/php-src/pull/1494 – Jens A. Koch Oct 21 '15 at 19:34
up vote 12 down vote accepted

No, 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.

share|improve this answer
2  
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

Update: visibility modifiers for constants have been added in PHP 7.1 (to be released in December 2016 according to PHP's release policy). See the RFC : Support Class Constant Visibility.

share|improve this answer

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

share|improve this answer

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

public static $variable = "abc";

but sadly final is not allowed here.

share|improve this answer

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

share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.