模拟 Ruby “splat” 的最佳方式PHP 函数签名中的运算符【方法重载】

发布于 2024-09-16 04:27:25 字数 285 浏览 0 评论 0原文

在 Ruby 中

def my_func(foo,bar,*zim)
  [foo, bar, zim].collect(&:inspect)
end

puts my_func(1,2,3,4,5)

# 1
# 2
# [3, 4, 5]

在 PHP 中 (5.3)

function my_func($foo, $bar, ... ){
  #...
}

在 PHP 中执行此操作的最佳方法是什么?

In Ruby

def my_func(foo,bar,*zim)
  [foo, bar, zim].collect(&:inspect)
end

puts my_func(1,2,3,4,5)

# 1
# 2
# [3, 4, 5]

In PHP (5.3)

function my_func($foo, $bar, ... ){
  #...
}

What's the best way to to do this in PHP?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请叫√我孤独 2024-09-23 04:27:25

从与此相关的另一个问题复制我的答案:

现在可以 PHP 5.6.x,使用 ... 运算符(也
在某些语言中称为 splat 运算符):

示例:

function addDateIntervalsToDateTime( DateTime $dt, DateInterval ...$intervals )
{
    foreach ( $intervals 作为 $interval ) {
        $dt->add($interval);
    }
    返回$dt;
}

Copying my answer from another question related to this:

This is now possible with PHP 5.6.x, using the ... operator (also
known as splat operator in some languages):

Example:

function addDateIntervalsToDateTime( DateTime $dt, DateInterval ...$intervals )
{
    foreach ( $intervals as $interval ) {
        $dt->add( $interval );
    }
    return $dt;
}
执妄 2024-09-23 04:27:25

尝试

Ruby 代码片段的 PHP 版本

function my_func($foo, $bar)
{
    $arguments = func_get_args();
    return array(
        array_shift($arguments),
        array_shift($arguments),
        $arguments
    );
}
print_r( my_func(1,2,3,4,5,6) );

或仅

function my_func($foo, $bar)
{
    return array($foo , $bar , array_slice(func_get_args(), 2));
}

给出

Array
(
    [0] => 1
    [1] => 2
    [2] => Array
        (
            [0] => 3
            [1] => 4
            [2] => 5
            [3] => 6
        )
)

请注意,func_get_args() 将返回传递给函数的所有参数,而不仅仅是那些不在签名中的参数。另请注意,您在签名中定义的任何参数都被视为必需的,如果它们不存在,PHP 将发出警告。

如果您只想获取剩余的参数并在运行时确定,您可以使用 ReflectionFunction API 用于读取签名中的参数数量和 array_slice 仅包含附加参数的完整参数列表,例如

function my_func($foo, $bar)
{
    $rf = new ReflectionFunction(__FUNCTION__);
    $splat = array_slice(func_get_args(), $rf->getNumberOfParameters());
    return array($foo, $bar, $splat);
}

为什么有人会希望仅使用 func_get_args() 是超出了我的范围,但它会起作用。更直接的是通过以下任何一种方式访问​​参数:

echo $foo;
echo func_get_arg(0); // same as $foo
$arguments = func_get_args();
echo $arguments[0]; // same as $foo too

如果您需要记录变量函数参数,PHPDoc 建议使用

/**
 * @param Mixed $foo Required
 * @param Mixed $bar Required
 * @param Mixed, ... Optional Unlimited variable number of arguments
 * @return Array
 */

希望有帮助。

Try

  • func_get_args — Returns an array comprising a function's argument list

PHP Version of your Ruby Snippet

function my_func($foo, $bar)
{
    $arguments = func_get_args();
    return array(
        array_shift($arguments),
        array_shift($arguments),
        $arguments
    );
}
print_r( my_func(1,2,3,4,5,6) );

or just

function my_func($foo, $bar)
{
    return array($foo , $bar , array_slice(func_get_args(), 2));
}

gives

Array
(
    [0] => 1
    [1] => 2
    [2] => Array
        (
            [0] => 3
            [1] => 4
            [2] => 5
            [3] => 6
        )
)

Note that func_get_args() will return all arguments passed to a function, not just those not in the signature. Also note that any arguments you define in the signature are considered required and PHP will raise a Warning if they are not present.

If you only want to get the remaining arguments and determine that at runtime, you could use the ReflectionFunction API to read the number of arguments in the signature and array_slice the full list of arguments to contain only the additional ones, e.g.

function my_func($foo, $bar)
{
    $rf = new ReflectionFunction(__FUNCTION__);
    $splat = array_slice(func_get_args(), $rf->getNumberOfParameters());
    return array($foo, $bar, $splat);
}

Why anyone would want that over just using func_get_args() is beyond me, but it would work. More straightforward is accessing the arguments in any of these ways:

echo $foo;
echo func_get_arg(0); // same as $foo
$arguments = func_get_args();
echo $arguments[0]; // same as $foo too

If you need to document variable function arguments, PHPDoc suggest to use

/**
 * @param Mixed $foo Required
 * @param Mixed $bar Required
 * @param Mixed, ... Optional Unlimited variable number of arguments
 * @return Array
 */

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文