Return by reference in PHP

后端 未结 3 1610
礼貌的吻别
礼貌的吻别 2020-12-14 06:23

I tried Googling, tried PHP Documentation, searched Stack Overflow for an answer but couldn\'t find anything satisfactory. I was reading a book in which author have made use

3条回答
  •  没有蜡笔的小新
    2020-12-14 07:16

    Easy rule: if you place an & in front of a function name in PHP you can use & when you call the function (ie. it's return value) as if it was a variable and not a function.

    That's it.


    It's easier to think of it as "PHP allowing you to reference the return value" rather then PHP returning by reference. As mentioned above you simply place an & to tell PHP you want this, if you don't place it and try do to $var =& somefunction() you will get the error "Only variables should be assigned by reference."

    To clear up some confusion with Jon's answer. There is actually no need to have two separate functions one returning by reference and one returning by value; outside of some project convention. The & only allows it to return by reference, it doesn't force it to return by reference.

    eg. same function used both for reference and non-reference assignments

    \header('content-type: text/plain');
    
    class Demo
    {
        protected $example = 'original value';
    
        function & example()
        {
            return $this->example;
        }
    }
    
    $demo = new Demo;
    
    #1 - pass by value
    
    $var1 = $demo->example();
    $var1 = 'var1 value';
    
    echo '$demo->example() => '.$demo->example().PHP_EOL;
    echo PHP_EOL;
    
    #2 - pass by reference
    
    $var2 =& $demo->example();
    $var2 = 'var2 value';
    
    echo '$demo->example() => '.$demo->example().PHP_EOL;
    echo '$var1 => '.$var1.PHP_EOL;
    echo '$var2 => '.$var2.PHP_EOL;
    echo PHP_EOL;
    
    #3 - altering other references
    
    $var3 =& $demo->example();
    $var3 = 'var3 value';
    
    echo '$demo->example() => '.$demo->example().PHP_EOL;
    echo '$var1 => '.$var1.PHP_EOL;
    echo '$var2 => '.$var2.PHP_EOL;
    echo '$var3 => '.$var3.PHP_EOL;
    echo PHP_EOL;
    
    exit;
    

    Output

    $demo->example() => original value
    
    $demo->example() => var2 value
    $var1 => var1 value
    $var2 => var2 value
    
    $demo->example() => var3 value
    $var1 => var1 value
    $var2 => var3 value
    $var3 => var3 value
    

    References are great however if you're not familiar with them please follow the following rules:

    • only consider using references if you are dealing with large array or similar non-object data structures
    • never reference an object (if you can avoid it)
    • only allow a function to pass by reference if the value is maintained in a static property or attribute on the object (ie. it's not temporary variable inside the function)

    Exceptions would obviously be very utilitarian uses (ie. when you actually want to share a value to simplify code for manipulating it).

    Again, NEVER REFERENCE OBJECTS (it's pointless!)

提交回复
热议问题