Doc #68725 [NEW]: Inconsistencies re passing variables by explicit reference
| From: | ianm at zeweb dot co dot uk | Date: | Fri, 02 Jan 2015 11:32:07 +0000 |
| Subject: | Doc #68725 [NEW]: Inconsistencies re passing variables by explicit reference | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-11813@lists.php.net to get a copy of this message | ||
From: ianm at zeweb dot co dot uk
Operating system: Linux
PHP version: 5.5.20
Package: *General Issues
Bug Type: Documentation Problem
Bug description:Inconsistencies re passing variables by explicit reference
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 bug report at https://bugs.php.net/bug.php?id=68725&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68725&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68725&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68725&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=68725&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=68725&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=68725&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=68725&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=68725&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=68725&r=support
Expected behavior: https://bugs.php.net/fix.php?id=68725&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=68725&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=68725&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=68725&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68725&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=68725&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=68725&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=68725&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=68725&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=68725&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=68725&r=mysqlcfg