How to avoid call-time pass-by-reference deprecated error in PHP?

后端 未结 5 690
情话喂你
情话喂你 2020-12-14 16:07

I\'m trying to reduce the warnings that are sent to my apache server log.

One warning is:

Call-time pass-by-reference has bee

相关标签:
5条回答
  • 2020-12-14 16:10

    You can set allow_call_time_pass_reference to true in your php.ini file. But it's a hack.

    0 讨论(0)
  • 2020-12-14 16:15

    You could pass an array with a reference in:

    public function takeScriptsWithMarker(array(&$lines, $marker))
    

    which should only take a small amount of refactoring at the other end.

    0 讨论(0)
  • 2020-12-14 16:19

    like noted above in a previous answer, the issue is at CALL time, not definition time.. so you could define a function as:

    function foo(&$var1,$var2,$var3=null){
        // procesing here
    }
    

    then call as:

    $return = foo($invar1,$invar2);
    

    your first invar is passed by reference, second one is not.

    the error appears when you try to call like so:

    $return = foo(&$invar1,$invar2);
    
    0 讨论(0)
  • 2020-12-14 16:32

    You could pass in the array, let it manipulate it, and then "return" it, instead of messing with the original reference. It shouldn't be too hard to just include a return and assignment.

    public function takeScriptsWithMarker($lines, $marker) {
        //...
        return $lines;
    }
    

    Usage:

    $lines = takeScriptsWithMarker($lines, $marker);
    
    0 讨论(0)
  • 2020-12-14 16:36

    Actually, there's no problem with the way you define the function. Is a problem with the way you call the function. So for your example, instead of calling it like:

    takeScriptsWithMarker(&$lines, $marker);
    

    You'd call it like:

    takeScriptsWithMarker($lines, $marker); // no ampersands :)
    

    So the feature is still available. But I don't know the reason behind this change.

    0 讨论(0)
提交回复
热议问题