【问题标题】:PHP SET default argument in function as static variablePHP SET 函数中的默认参数作为静态变量
【发布时间】:2015-01-27 02:01:05
【问题描述】:

是否可以将类函数中参数的默认值设置为静态变量 感谢您提前提供的帮助!

class UserControl {

    public static $CurrentUID;

    public static function isUserExist($CurrentUID = UserControl::$CurrentUID){

     ....

    }
}

【问题讨论】:

  • 不可能,如PHP documentationThe default value must be a constant expression, not (for example) a variable, a class member or a function call.中所说
  • 啊……当然只做字符串

标签: php static arguments default default-value


【解决方案1】:

在这种情况下,您可以采取一种解决方法:

public static function isUserExist($CurrentUID = false)
{
   if(!$CurrentUID)
      $CurrentUID = UserControl::$CurrentUID;
   ....
}

【讨论】:

  • 不幸的是我的函数是在一个紧密的循环中调用的,所以条件是昂贵的
猜你喜欢
  • 2017-02-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-03
  • 2019-11-29
  • 1970-01-01
  • 2016-07-17
  • 2018-09-15
  • 2011-05-31
相关资源
最近更新 更多