4

Please consider this code:

    class App {

        const ALERT_ERROR=1;
        const ALERT_WARN=2;
        const ALERT_INFO=3;

        public static function alert($title,$type=ALERT_ERROR) {        
            switch ($type){
                default:                
                case static::ALERT_ERROR:
                    $class="alert-danger"; break;
                case static::ALERT_WARN:
                    $class="alert-warning"; break;
                case static::ALERT_INFO:
                    $class="alert-info"; break;
            }
            ...
        }
    }

I have some trouble with the static::constant syntax inside the switch. I some PHP webserver it is recognized as correct value (as defined in const ALERT_ERROR) in other server I had to remove the static:: prefix and leave only the constant name. But with this way the first webserver doesn't work...

The first php version is 5.4.7 the second is 5.4.37... but I don't think this is version problem.

The problem is that if I run this code:

App::alert("test",App::ALERT_INFO);

the $class is set a "alert-danger" as default, and the App::ALERT_INFO constant is not recognized. If I add static:: prefix the constant is recognized from one webserver and not from the other and viceversa if I remove it.

The notice thrown is: Use of undefined constant ALERT_ERROR - assumed 'ALERT_ERROR'

EDIT after the answer

The problem was not in the switch cases but in the default parameter. That was assumed as "ALERT_ERROR" string and so only the default switch was got.

6
  • 1
    What does "doesn't work" mean? Commented Mar 1, 2015 at 11:20
  • I will better explain it in the question... sorry Commented Mar 1, 2015 at 11:21
  • Before closing your question try to explain how exactly it's not working Commented Mar 1, 2015 at 11:21
  • Do you have any error message? Commented Mar 1, 2015 at 11:25
  • If I add static:: prefix - where? Commented Mar 1, 2015 at 11:27

1 Answer 1

14

You have to add the static keyword to the constant used as default value.

public static function alert($title, $type=static::ALERT_ERROR) {
  // ....
}

But static is only determined at runtime. So if static behaviour is really needed then:

public static function alert($title, $type=null) {
  if ($type === null) {
      $type = static::ALERT_ERROR;
  }
  // ...
}

If static is not really necessary then replace all static:: as self::

   public static function alert($title, $type=self::ALERT_ERROR) {        
    switch ($type){
        default:                
        case self::ALERT_ERROR:
            $class="alert-danger"; break;
        case self::ALERT_WARN:
            $class="alert-warning"; break;
        case self::ALERT_INFO:
            $class="alert-info"; break;
    }
    ....
}
Sign up to request clarification or add additional context in comments.

5 Comments

Fatal error: "static::" is not allowed in compile-time constants
This seems to be the answer, but I have to set a different param default value.
Ok. Do you really need static? If not use self. static is for inheritance override
Please edit answer to reflect correct answer, as using static here gives fatal error!
In this particular case I can also use null as default param because the it is used only in the case switch so it is catched by the "default" case. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.