PHP:常量和常量位分隔符作为方法的默认参数
我正在创建一个类方法,并希望有一个包含常量的默认参数值:
<?php
class mq_series_client{
function get($message_options = array('Options' => MQSERIES_MQGMO_FAIL_IF_QUIESCING | MQSERIES_MQGMO_WAIT, 'WaitInterval' => 500)){
}
}
但是我收到了解析错误:语法错误,意外的“|”
我可以这样做:
<?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);
}
}
}
但它看起来不太干净。我希望第一种方法能够奏效!
有没有更好的“正确”方法来做到这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据此页面,第一个选项似乎无效:
尝试了一下,看起来表达式不被接受,例如:
它在
+
上被阻塞——大概是因为它不是一个“常量表达式”。It looks like the first option is not valid, as according to this page:
Experimenting a bit, it looks like expressions are not accepted, like:
which chokes on the
+
-- presumably because it is not a “constant expression.“