将所有参数传递给另一个函数

时间:2022-02-21 07:04:52

I have two functions like this:

我有两个这样的功能:

function mysql_safe_query($format) {
    $args = array_slice(func_get_args(),1);
    $args = array_map('mysql_safe_string',$args);
    $query = vsprintf($format,$args);
    $result = mysql_query($query);
    if($result === false) echo '<div class="mysql-error">',mysql_error(),'<br/>',$query,'</div>';
    return $result;
}

function mysql_row_exists() {
    $result = mysql_safe_query(func_get_args());
    return mysql_num_rows($result) > 0;
}

The problem is that the second function won't work because it passes the args to the first one as an array, when it expects them as different parameters. Is there any way to get around this, preferably without modifying mysql_safe_query?

问题是第二个函数不起作用,因为它将args作为数组传递给第一个函数,当它期望它们作为不同的参数时。有没有办法解决这个问题,最好不要修改mysql_safe_query?

3 个解决方案

#1


22  

How about using:

如何使用:

$args = func_get_args();
call_user_func_array('mysql_safe_query', $args);

#2


5  

N.B. In PHP 5.6 you can now do this:

注:在PHP 5.6中,您现在可以执行此操作:

function mysql_row_exists(...$args) {
    $result = mysql_safe_query(...$args);
    return mysql_num_rows($result) > 0;
}

Also, for future readers, mysql_* is deprecated -- don't use those functions.

此外,对于未来的读者,不推荐使用mysql_ * - 请勿使用这些功能。

#3


0  

Depending on the situation, following might also work for you and might be a little faster.

根据具体情况,以下内容可能对您有用,可能会更快一些。

function  mysql_safe_query($format) {
    $args = func_get_args();
    $args = is_array($args[0]) ? $args[0] : $args; // remove extra container, if needed
    // ...

Which now allows for both types of calling, but might be problematic if your first value is supposed to be an actual array, because it would be unpacked either way.

现在允许两种类型的调用,但如果你的第一个值应该是一个实际的数组可能会有问题,因为它将以任何方式解压缩。

You could additionally check on the length of your root-array, so it might not be unpacked if if there are other elements, but as mentioned: It is not really that "clean" in general, but might be helpful and fast :-)

您还可以检查根数组的长度,因此如果有其他元素,可能不会解压缩,但如上所述:它通常不是“干净”,但可能有用且快速:-)

#1


22  

How about using:

如何使用:

$args = func_get_args();
call_user_func_array('mysql_safe_query', $args);

#2


5  

N.B. In PHP 5.6 you can now do this:

注:在PHP 5.6中,您现在可以执行此操作:

function mysql_row_exists(...$args) {
    $result = mysql_safe_query(...$args);
    return mysql_num_rows($result) > 0;
}

Also, for future readers, mysql_* is deprecated -- don't use those functions.

此外,对于未来的读者,不推荐使用mysql_ * - 请勿使用这些功能。

#3


0  

Depending on the situation, following might also work for you and might be a little faster.

根据具体情况,以下内容可能对您有用,可能会更快一些。

function  mysql_safe_query($format) {
    $args = func_get_args();
    $args = is_array($args[0]) ? $args[0] : $args; // remove extra container, if needed
    // ...

Which now allows for both types of calling, but might be problematic if your first value is supposed to be an actual array, because it would be unpacked either way.

现在允许两种类型的调用,但如果你的第一个值应该是一个实际的数组可能会有问题,因为它将以任何方式解压缩。

You could additionally check on the length of your root-array, so it might not be unpacked if if there are other elements, but as mentioned: It is not really that "clean" in general, but might be helpful and fast :-)

您还可以检查根数组的长度,因此如果有其他元素,可能不会解压缩,但如上所述:它通常不是“干净”,但可能有用且快速:-)