【问题标题】:Reference parameters (&$a) in functions, good or bad?函数中的引用参数(&$a),好还是坏?
【发布时间】:2012-04-23 15:16:26
【问题描述】:

我们编写了两个验证函数,一个如下所示(一次获取所有字段):

function check_fields(&$arr,&$msg,$types,$lens,$captions,$requireds) {}

另一个函数如下所示:

function is_valid($field=NULL,$type=0 ,$length=0,$required=true) {}

第一个函数有几行代码,并且大大减少了代码行(大约30-35行甚至更多),另一方面,第二个没有引用的函数增加了代码行(大约30-35行甚至更多) .

我们必须为要验证的每个字段调用第二个函数,但第一个函数 (check_fields) 反之亦然。 我很久以前在一篇文章中读到,从性能角度来看,带有引用参数的函数很糟糕。

现在我们不知道要使用哪个函数。从性能的角度来看,哪个更好?

【问题讨论】:

    标签: php function parameter-passing pass-by-reference


    【解决方案1】:

    当你打电话时,你只记得这个:

       <?php 
        $a = 1 ;
        echo "As initial, the real 'a' is..".$a."<br/>";
        $b = &$a ; // it's meaning $b has the same value as $a, $b = $a AND SO $a = $b (must be)
        $b += 5;
        echo "b is equal to: ".$b." and a equal to: ".$a."<br/>";
        echo " Now we back as initial, when a=1... (see the source code) <br/>";
        $a = 1 ;
        echo "After we back : b is equal to: ".$b." and a equal to: ".$a."<br/>";
        echo "Wait, why b must follow a? ... <br/> ..what about if we change b alone? (see the source code)<br/>";
        $b = 23; 
        echo "After we change b alone, b equal to: ".$b." and a equal to: ".$a."<br/>";
        echo " WHAT ?? a ALSO CHANGED ?? a and b STICK TOGETHER?!! </br>" ;
        echo "to 'clear this, we use 'unset' function on a (see the source code)<br/> ";
        unset($a);
        $b = 66;
        $a = 1;
        echo "Now, after unset,b is equal to: ".$b." and a equal to: ".$a."<br/>"; 
        ?>
    

    输出将是:

    As initial, the real 'a' is..1
    After the Reference operator...(see the source code)
    b is equal to: 11 and a equal to: 11
    Now we back as initial... (see the source code) 
    After we back : b is equal to: 1 and a equal to: 1
    Wait, why b must follow a? ... 
    ..what about if we change b alone? (see the source code)
    After we change b alone, b equal to: 23 and a equal to: 23
    WHAT ?? a ALSO CHANGED ?? a and b STICK TOGETHER?!! 
    to 'clear this, we use 'unset' function on a (see the source code)
    Now, after unset,b is equal to: 66 and a equal to: 1
    

    评论:

    为什么$a 发生了变化?那是因为&amp;(与号运算符)将&amp; 作为参考变量,因此它存储在“临时内存”中。当您初始化$b= &amp;$a 时,请参阅我的评论$b = $a 以及 $a = $b,这意味着每当您修改$b 时,$a 也会被修改,反之亦然。 他们被锁住了!这就是引用运算符的简单含义。

    也许一开始感觉很迷惑,但是一旦你成为这方面的专家,你就可以像这样处理这个操作符来做一个“切换”功能:

    <?php
    
    function bulbswitch(&$switch)
    {
        $switch = !$switch;    
    
    }
    
    function lightbulb($a)
    {
        if ($a == true) { echo 'a is On <br/>';}
        else { echo 'a is Off <br/>';}
    }
    
    $a = true ;
    echo 'At the begining, a is On, then.... <br/>';
    
    bulbswitch($a);
    lightbulb($a);
    bulbswitch($a);
    lightbulb($a);
    bulbswitch($a);
    lightbulb($a);
    bulbswitch($a);
    lightbulb($a);
    bulbswitch($a);
    lightbulb($a);
    bulbswitch($a);
    lightbulb($a);
    ?>
    

    输出将是:

    At the begining, a is On, then.... 
    a is Off 
    a is On 
    a is Off 
    a is On 
    a is Off 
    a is On
    

    【讨论】:

    • 我试图编辑这个,但是你的编辑覆盖了我的……你必须缩进普通文本,否则一切都会显示为代码。
    • 谢谢,但现在我已经解决了。看到了吗?
    • 好吧,一切都还在代码块中,所以没有;)
    • edit这个答案并删除多余的缩进。另请参阅stackoverflow.com/editing-help
    【解决方案2】:

    好吧,我想我在一些网络搜索后自己得到了答案:

    Do not use PHP references

    【讨论】:

    • 任何对你有用的东西。如果您想遵循正确的 OO,则创建类并传递该类的实例而不是六个参数。对象通过 php5 中的 ref 传递,因此您可以根据需要对其进行更改。使用类型转换来确保传递正确的类。在我看来,你专注于错误的事情......如果你想优化你的代码专注于数据库调用和磁盘读写。
    【解决方案3】:

    使用更易于使用且更易于维护的解决方案。 您在谈论微优化,这几乎没有用。


    使用引用,因为在您的情况下,它是更简单的解决方案并且需要更少的代码。

    【讨论】:

    • 特别是当他为了实现微优化而将代码降低到可读性较低的水平时!
    • 这不是我的答案,我其实很想知道哪个更好,为什么,请不要这样回答。
    猜你喜欢
    • 2014-07-23
    • 1970-01-01
    • 2011-10-08
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 2011-01-16
    • 2010-11-26
    相关资源
    最近更新 更多