静态方法中 splat 运算符的默认参数值
Default parameter value for splat operator in static method
我正在尝试将一些 php 函数重写为标准 class 库作为静态方法,以便我的开发团队能够理解 php 函数。
这是我目前拥有的:
class StringUtil
{
public static function sprintf ($format, $args = null, ... $_)
{
return sprintf($format,$args,$_);
}
}
有了这个方法声明,我应该能够正确地使用 splat operator
。
我的问题是如果没有超过 $args
的参数使用,那么 splat operator
应该会失败,因为它是按要求声明的。
我正在寻找的是这样的东西,null
值作为 splat operator
:
的默认参数传递
class StringUtil
{
public static function sprintf ($format, $args = null, ... $_ = null)
{
return sprintf($format,$args,$_);
}
}
class StringUtil
{
public static function sprintf($format)
{
$args = func_get_args();
$fmt = array_shift($args);
return vsprintf($fmt, $args);
}
}
更新:
class StringUtil
{
public static function sprintf($fmt)
{
return call_user_func_array('sprintf', func_get_args());
}
}
我正在尝试将一些 php 函数重写为标准 class 库作为静态方法,以便我的开发团队能够理解 php 函数。
这是我目前拥有的:
class StringUtil
{
public static function sprintf ($format, $args = null, ... $_)
{
return sprintf($format,$args,$_);
}
}
有了这个方法声明,我应该能够正确地使用 splat operator
。
我的问题是如果没有超过 $args
的参数使用,那么 splat operator
应该会失败,因为它是按要求声明的。
我正在寻找的是这样的东西,null
值作为 splat operator
:
class StringUtil
{
public static function sprintf ($format, $args = null, ... $_ = null)
{
return sprintf($format,$args,$_);
}
}
class StringUtil
{
public static function sprintf($format)
{
$args = func_get_args();
$fmt = array_shift($args);
return vsprintf($fmt, $args);
}
}
更新:
class StringUtil
{
public static function sprintf($fmt)
{
return call_user_func_array('sprintf', func_get_args());
}
}