3

Im creating a class method and want to have a default argument value that contains constants:

<?php
class mq_series_client{
    function get($message_options = array('Options' => MQSERIES_MQGMO_FAIL_IF_QUIESCING | MQSERIES_MQGMO_WAIT, 'WaitInterval' => 500)){

    }
}

However I'm getting a Parse error: syntax error, unexpected '|'

I could do this:

<?php
class mq_series_client{
    function get(Array $message_options = null){
        if(!isset($message_options)){
           $message_options = array('Options' => MQSERIES_MQGMO_FAIL_IF_QUIESCING | MQSERIES_MQGMO_WAIT, 'WaitInterval' => 500);
        }
    }
}

But it doesn't seem very clean. I wish the first way would work!

Is there a better "correct" way to do this?

Charles
  • 50,943
  • 13
  • 104
  • 142
Paul Wieland
  • 765
  • 2
  • 10
  • 29

1 Answers1

2

It looks like the first option is not valid, as according to this page:

The default value must be a constant expression, not (for example) a variable, a class member or a function call.

Experimenting a bit, it looks like expressions are not accepted, like:

<?php
    function get($options = array('test' => 1+1)) {

    }
}
?>

which chokes on the + -- presumably because it is not a “constant expression.“

Sébastien Le Callonnec
  • 26,254
  • 8
  • 67
  • 80
  • Thanks Sebastien, that better explains why my first attempt failed. Quite frustrating actually, it would be much cleaner if it worked that way. – Paul Wieland Jan 24 '11 at 13:06