简体   繁体   English

PHP通过引用混淆

[英]PHP Pass by reference confusion

To cut a long story short I have the following function as part of my framework: 简而言之,我有以下功能作为我的框架的一部分:

public function use_parameters()
{
    $parameters = func_get_args();

    $stack = debug_backtrace();

    foreach($stack[0]['args'] as $key => &$parameter)
    {
        $parameter = array_shift($this->parameter_values);
    }
}

Where $this->parameter_values = array('value1', 'value2', 'value3', 'value4', 'value5', ...); 其中$ this-> parameter_values = array('value1','value2','value3','value4','value5',...);

Which is used in the following context: 在以下上下文中使用:

$instance->use_parameters(&$foo, &$bah);

Assigning: 分配:

$foo = 'value1';
$bah = 'value2';

Calling it again 再次打电话给它

$instance->use_parameters(&$something); 

Will set 将设置

$something = 'value3'

and so on. 等等。

As of 5.3 it would return a 'Deprecated: Call-time pass-by-reference has been deprecated' warning. 从5.3开始,它将返回“已弃用:调用时间传递引用已被弃用”警告。 In an effort to conform to the 5.3 way of working I removed the &'s resulting in: 为了符合5.3的工作方式,我删除了导致:

$instance->use_parameters($foo, $bah);

This unfortunately has caused the arguments not to be set and I'm struggling to come up with a solution. 不幸的是,这导致了这些论点没有确定,我正在努力想出一个解决方案。

For what its worth I'm running PHP v5.3.3-7 on Apache/2.2.16 (Debian) 值得我在Apache / 2.2.16(Debian)上运行PHP v5.3.3-7

Any help would be greatly appreciated. 任何帮助将不胜感激。

You can't do that in PHP and you are abusing the concept of references. 你不能用PHP做到这一点,你滥用参考的概念。 You have to specify the reference arguments explicitly, albeit with default values. 您必须明确指定引用参数,尽管使用默认值。 However , you don't want to use NULL as a default as this is what an unassigned reference variable will be set to. 但是 ,您不希望将NULL用作默认值,因为这是未分配的引用变量将设置为的值。 So you need to define some constant that you know is not going to be used as a parameter, and now the code will look something like 所以你需要定义一些你知道不会被用作参数的常量,现在代码看起来像

    const dummy="^^missing^^";

    public function use_parameters(&$a, &$b=self::dummy, &$c=self::dummy ) {
        $a=array_shift($this->parameter_values);
        if($b!==self::dummy) $b=array_shift($this->parameter_values);
        if($c!==self::dummy) $c=array_shift($this->parameter_values);
        # add $d,$e,$f,... as required to a sensible max number
    }

Note that because you are using references properly, you don't need the debug_backtrace() botch. 请注意,因为您正确使用引用,所以不需要debug_backtrace()。

You can't do that in PHP 5 or ... as you've seen you can, but it's deprecated and raises warnings. 你不能在PHP 5中做到这一点,或者......你已经看过你可以做到这一点,但它已被弃用并引发警告。 You'll have to either have a predefined max count of function arguments or use an array: 您必须具有预定义的函数参数最大计数或使用数组:

public function use_parameters(&$arg1, &$arg2 = NULL, &$arg3 = NULL)
{
    // if ($arg2 !== NULL)
    // if ($arg3 !== NULL)
}

$parameters = array(0 => &$foo, 1 => &$bah);

public function use_parameters($args)
{
    // Handle the array elements
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM