通过引用pickle调用时间传递


Call-time pass-by-reference pickle

让我们自己陷入了困境。我们的主机已经将php升级到5.4,并且我们仍然在一个类中运行一些代码(我们没有编写),该类通过引用将参数传递给函数,看起来如下:

$func_args = '';
// Make $row[0], $row[1] accessible by using $C1, $C2 etc.
foreach ($row as $k => $v)
{
    ${'C'.($k+1)} = $v;
    $func_args .= "&'$C".($k+1).",";
}
// Give the user a chance to tweak the results with any function of their choice
// tweak functions are registered with $ez_results->register_function('func_name');
if ( is_array($this->tweak_functions) )
{
    // Tweak results with each registered function
    foreach ( $this->tweak_functions as $tweak_function )
    {
        // If function C1, C2, etc exists then run it
        if ( function_exists($tweak_function) )
        {
            eval("$tweak_function(".substr($func_args,0,-1).");");
        }
    }
}

函数在类中进一步注册如下:

var $tweak_functions = array('tweak_results');
function register_function($function_name)
{
    $this->tweak_functions[] = $function_name;
}

这些函数是在外部PHP文件上定义的,如下所示:

function results_manipulation($news_id,$news_name,$news_seoname,$news_date2,$news_summary,$news_article,$news_url,$image_name,$image_thumb,$news_categories)
{
    global $i;
    if(!empty($image_thumb) && $i < 3 && empty($_GET['pg']) ){
        $image_thumb = '<div class="newsthumb" style="background-image:url('.$image_thumb.')" title="'.$image_name.'"></div>';
    }else{
        $image_thumb = '';
    }
    $i++;
}

我研究了很多类似的问题,并试图找到一种替换代码并保持一切正常工作的方法,但没有成功。有人能给我指正确的方向吗?

非常感谢

我会更改所有调整函数的签名,使其在参数列表中包含引用符号,并将其从参数列表中删除。

  function someTweakFunction(&$a, &$b, &$c);

此外,如果您可以删除eval代码,那将是一件好事。在这种特殊情况下,它看起来并不危险,但也不必要。您可以改用call_user_func_array。

构建参数列表时,请创建一个参数数组,而不是一个参数字符串。

$func_args = array();
// Make $row[0], $row[1] accessible by using $C1, $C2 etc.
foreach ($row as $k => $v)
{
    $func_args[] = &$v;
}
if ( is_array($this->tweak_functions) )
{
    // Tweak results with each registered function
    foreach ( $this->tweak_functions as $tweak_function )
    {
        // If function C1, C2, etc exists then run it
        if ( function_exists($tweak_function) )
        {
            call_user_func_array($tweak_function, $func_args);
        }
    }
}

我遇到了同样的问题(使用ez_results),我解决了它。也许有人会喜欢这个有用的东西。

这条线路

//old    
$func_args .= "&'$C".($k+1).",";

更改为:

//new
$func_args .= "'$C".($k+1).",";

此外,您在ezr->register_function("my_function")中使用的函数

//old
my_function($arg1, $arg2, $arg3...)

必须更改(在每个参数前面添加"&"):

//new
my_function(&$arg1, &$arg2, &$arg3...)

我最终完全重新编写了函数,并用重新编写的代码更新了所有网站。

感谢所有最终提出建议的人,尽管重新设计我们所拥有的只是延长了痛苦:在某个阶段,需要进行全面的重写。