質問

Derick Rethans has an old article that says:

Please do note that it is harmful not to accept a reference from a function that returns a reference. In some cases, PHP will get confused and cause memory corruptions which are very hard to find and debug. It is also not a good idea to return a static value as reference, as the PHP engine has problems with that too. In PHP 4.3, both cases can lead to very hard to reproduce bugs and crashes of PHP and the web server. In PHP 5, this works all a little bit better. Here you can expect a warning and it will behave “properly”.

Does it mean that in PHP 5 we are allowed to ignore the returned reference from a function?

By that, I mean this:

function &GetRef(&$array){
    $item =& $array[0];
    return $item;
}

$array = array(0, 1, 2);
$item =& GetRef($array); /* Normal usage of the function using assign by reference
                            also known as "accepting" the reference. */

$item = GetRef($array); /* Notice that here we didn't assign by reference.
                           Are we allowed to ignore the returned reference
                           and simply do normal assignment? */

The PHP Manual states:

Unlike parameter passing, here [return by reference] you have to use & in both places - to indicate that you want to return by reference, not a copy, and to indicate that reference binding, rather than usual assignment, should be done for $myValue.

It doesn't explicitly say that we must accept the returned reference.

Does it mean that we are free to ignore returned references?

役に立ちましたか?

解決

As discussed in the comments, you should generally ignore at least that section in the linked article, if not the entire thing.

The article talks about references in the context of PHP 4.3, released in December, 2002 and EOL'd at the end of 2007. PHP 4 should never be used today. As a general rule, when it comes to learning about working with PHP, you should not trust any article that targets PHP versions older than 5.2 (as of mid-2013).

PHP 5.0 features Zend Engine 2, a new virtual machine on which PHP runs. This is where references are implemented. 5.1 introduces some backwards-incompatible changes with regard to manipulation of return values. 5.3 introduces real garbage collection and deprecates both call-time pass-by-reference and assigning new by reference. These important changes are not addressed by that prehistoric article.

Does it mean that in PHP 5 we are allowed to ignore the returned reference from a function?

Yes. Modern PHP versions have no problem with discarding the return value of any function, reference or not. If you encounter behavior that seems to contradict this expectation, create a reduced test case and file a bug with the PHP maintainers.

Also, think twice before using references in your code. Passing around references will not save time, will not save memory and will not increase performance except in rare cases. Use them sparingly to keep complexity under control.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top