Doc #68725 [Nab]: Inconsistencies re passing variables by explicit reference
| From: | ianm at zeweb dot co dot uk | Date: | Fri, 02 Jan 2015 22:30:53 +0000 |
| Subject: | Doc #68725 [Nab]: Inconsistencies re passing variables by explicit reference | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-11815@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68725&edit=1
ID: 68725
User updated by: ianm at zeweb dot co dot uk
Reported by: ianm at zeweb dot co dot uk
Summary: Inconsistencies re passing variables by explicit
reference
Status: Not a bug
Type: Documentation Problem
Package: *General Issues
Operating System: Linux
PHP Version: 5.5.20
Block user comment: N
Private report: N
New Comment:
It would seem to me that if [$var] means you can put a variable $var in your call, then commonsense
says that [&$var] must mean likewise. Otherwise it has to be assumed that no symbol in any
definition necessarily has its standard meaning.
Though, even if examples 1 and 2 are deemed intentional notation, that does not answer why foreach()
uses what seems to be explicitly illegal syntax under the new rules. I think I can answer that
myself, which is that it has been realised that the prohibition on pass-by-reference is actually
impractical and unworkable. In which case, IMHO it would be better to reinstate it than have this
paradox.
Previous Comments:
------------------------------------------------------------------------
[2015-01-02 18:26:24] aharvey@php.net
The function definition shown in the descriptions is just that: the definition. If you were writing
the functions in PHP, you'd have to include the & to indicate that the parameter is
by-reference, so this is actually consistent â those ampersands don't imply anything
about how you call the function (where, as you correctly note, you should not include an ampersand,
since call time pass by reference was removed some time ago).
This may be a situation where we could have more examples of calling those functions with the
optional reference parameters, but in terms of the prototypes (which is what this bug is about),
this is correct.
------------------------------------------------------------------------
[2015-01-02 11:32:05] ianm at zeweb dot co dot uk
Description:
------------
php 5.4 or later. This concerns inconsistent handling of explicit passing of variables by reference.
Some functions permit this when the documentation states that they should not, others the reverse.
Examples:
mixed preg_replace_callback ( mixed $pattern , callable $callback , mixed $subject [, int $limit =
-1 [, int &$count ]] )
mixed str_ireplace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
-Which seems to imply that $count SHOULD be passed by explicit reference. In fact, $count is
automatically passed by reference, and adding an ampersand will cause a critical error in php 5.4 or
later.
For these two functions the issue is fixed by removing the ampersand, since the argument is always
passed by reference.
Quoting the docs on http://php.net/manual/en/language.references.pass.php:
"Note: There is no reference sign on a function call - only on function definitions. Function
definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will
get a warning saying that "call-time pass-by-reference" is deprecated when you use &
in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will
raise a fatal error."
Yet, the documentation on foreach() gives the syntax as:
foreach (array_expression as $value)
and goes on the say that:
"In order to be able to directly modify array elements within the loop precede $value with
&. In that case the value will be assigned by reference."
-Which seems to be in total violation of the docs!
The foreach() issue is harder to overcome since passing by value (removing the ampersand) would not
achieve the same result. The worry here is that although (the technically illegal) passing by
reference works at present, it may cease to work at some point in the future.
There almost certainly are many other examples of such.
I understand that the change in php 5.4 -forbidding the passing of variables by explicit reference-
was made 'in the interests of consistency' - but I fail to see how this is
'consistent' behaviour. On the contrary, it CREATES major inconsistencies. In principle
the whole of the php documentation needs reviewing to find and correct all of these inconsistencies,
a very major task.
Test script:
---------------
foreach ($newusers as &$thisuser){
$username=$thisuser['section_name'];
if ($thisuser['privelege']==-1){
$thisuser['is_deleted']=true;
continue;
}
}
Expected result:
----------------
Ought to work in 5.3 but should bug-out in 5.4 or later.
Actual result:
--------------
Works in all tested php versions.
The question here is whether it is, or is not, safe to continue using such code?
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68725&edit=1