PHP: Constants & Bitwise Separator as Default Argument for Method

I am creating a class method and want to have a default argument value containing constants:

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

    }
}

However, I get 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 this does not seem very clean. I want the first way to work!

Is there a better “right” way to do this?

+3
source share
1 answer

It seems that the first option is invalid, as according to this page :

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

Experimenting a bit, it seems that expressions are not accepted, for example:

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

    }
}
?>

+ - , " ".

+2

Source: https://habr.com/ru/post/1786951/


All Articles