Php 调用时间传递引用pickle

Php 调用时间传递引用pickle,php,pass-by-reference,Php,Pass By Reference,让我们陷入了困境。我们的主机已将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).","; } //

让我们陷入了困境。我们的主机已将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).");");
        }
    }
}
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++;
}
函数在此类中进一步注册:

var $tweak_functions = array('tweak_results');

function register_function($function_name)
{
    $this->tweak_functions[] = $function_name;
}
函数在外部PHP文件上定义如下:

$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).");");
        }
    }
}
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++;
}
函数结果\u操作($news\u id、$news\u name、$news\u seoname、$news\u date2、$news\u summary、$news\u article、$news\u url、$image\u name、$image\u thumb、$news\u categories)
{
全球1美元;
如果(!empty($image_thumb)&&$i<3&&empty($\u GET['pg'])){
$image_thumb='';
}否则{
$image_thumb='';
}
$i++;
}
我已经研究了很多类似的问题,并试图找到一种方法来替换代码并保持一切正常,但没有成功。谁能给我指出正确的方向吗


非常感谢

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

  function someTweakFunction(&$a, &$b, &$c);
如果您可以删除eval代码,这也是一件好事。在这种特殊情况下,它看起来并不危险,但也没有必要。您可以改用call\u user\u func\u数组

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

$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_结果),我解决了它。也许有人会喜欢这个有用的

这条线

//old    
$func_args .= "&\$C".($k+1).",";
改为:

//new
$func_args .= "\$C".($k+1).",";
另外,您与ezr->register_函数(“my_函数”)一起使用的函数

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


如果有帮助的话,我可以上传完整的类。这是一些非常可怕的代码。。。第一部分真正要做的是一个简单的
call\u user\u func\u数组
,不需要
eval
调用和拼凑字符串。问题是,它调用的那些函数在调用时是否可以在没有引用的情况下工作?应该是这样的,所以只要删除
&
就可以了。我知道deceze非常可怕,老实说,我一直想重写它很多年了(大概18个月),但是当php5.4推出的时候,这个问题一夜之间就变成了一个真正的问题——我应该看到它的到来!该代码正在大量网站上使用,因此我需要一个快速的解决方案,并给我喘息的空间回去创建一个更强大的解决方案。您好Orangepill-非常感谢您的回复。我已经尝试了上面的更改,虽然不再存在致命错误,$tweak_函数中的参数似乎根本没有被操纵。i、 在我上面使用的示例中,$image\u thumb只会吐出数据库值(到图像的路径)。不确定我哪里出错了?@Studio4你确定要更改方法签名以通过引用接受参数吗?确实是这样-我实际上只使用了一个函数“results\u manipulation”,所以在我上面的示例中,我在每个参数中添加了一个符号。运行更改时,它显示为空,因此我在$func_args[]=&$v;之前添加了以下行${'C'($k+1)}=$v;这给了我一些输出,但它没有处理单独的更改。