Req #70278 [Com]: Allow null to be passed by reference
| From: | rowan dot collins at gmail dot com | Date: | Sun, 16 Aug 2015 13:19:58 +0000 |
| Subject: | Req #70278 [Com]: Allow null to be passed by reference | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-195244@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70278&edit=1
ID: 70278
Comment by: rowan dot collins at gmail dot com
Reported by: cyslider at posteo dot de
Summary: Allow null to be passed by reference
Status: Open
Type: Feature/Change Request
Package: *General Issues
PHP Version: Next Major Version
Block user comment: N
Private report: N
New Comment:
Nearly every function that "expects a reference merely for performance reasons" has been
written wrong - in many cases, pass by reference actually has worse performance (the compiler is
cleverer than people give it credit for). The language shouldn't be making such incorrect usage
easier.
There can be cases where you can avoid a copy-on-write using a reference param, but they should be
carefully considered, and are unlikely to be in frequently-called code where creating a variable
would be burdensome.
Most of the time, if a function is asking for something by reference, it should be because it's
expecting to write back to that variable, so passing a non-variable makes no sense. For this case,
the current behaviour is entirely correct.
Note that because your first example provides a default parameter value, you can just write
testFunc(); anyway if you're not interested in the reference parameter.
Previous Comments:
------------------------------------------------------------------------
[2015-08-15 15:06:12] cyslider at posteo dot de
Description:
------------
Please allow null - and preferable also any other value - to be directly passed into a function that
expects a reference.
If I want to pass 'null' by reference I currently have to do it like this:
function testFunc(Array &$array = null) {
// Do something
}
$null = null;
testFunc($null);
This is cumbersome.
Better would be to also allow:
testFunc(null);
I would suggest some automatical boxing in case a direct value is passed instead of a referenceable
variable. That way you could also pass strings directly into a function that expects a reference
merely for performance reasons.
function exec (&$sql) {
//Execute SQL
}
exec("Select 1");
Which currently throws an error.
Greetings,
CySlider
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70278&edit=1