Req #71758 [NEW]: can't initialize inline a pass-by-references variable parameter
| From: | vittorio dot zamparella at gmail dot com | Date: | Wed, 09 Mar 2016 15:36:44 +0000 |
| Subject: | Req #71758 [NEW]: can't initialize inline a pass-by-references variable parameter | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-199705@lists.php.net to get a copy of this message | ||
From: vittorio dot zamparella at gmail dot com
Operating system:
PHP version: 7.0.4
Package: Compile Warning
Bug Type: Feature/Change Request
Bug description:can't initialize inline a pass-by-references variable parameter
Description:
------------
While I perfectly understand why I cannot pass by reference a literal, I
whish that I could initialize a variable in the same place where I pass
it as a parameter.
MyFunction( $var2 = "two" );
I think it is more readable, understandable and inline with the syntax
of the language.
Maybe I'm missing some symple syntactic sugar to work around this (apart
from initializing the variable the line before).
I understand that the result of assignment is an anonimous literal with
containing the just assigned value (hence the warning), but I think that
the compiler could be just that little bit smarter to allow for such a
syntax, since the semantic is perfectly clear: assign the literal to the
variable, then pass the variable (and not its value to the function.
Also the algorithm is quite simple: if the actual parameter is a literal
and a variable is expected, if the literal is the product of an
assignment pass the left part of the assignment to the function.
Actually I don't know if the parser can trace back where a value is
coming from.
Thank you for your comments and explanantions.
Test script:
---------------
<?php
function MyFunction (&$parameter) {
echo $parameter."\n";
}
$var1 = 'one';
MyFunction( $var1 );
MyFunction( $var2 = "two" );
Expected result:
----------------
one
two
Actual result:
--------------
one
Notice: Only variables should be passed by reference in /in/iJAlU on
line 7
two
--
Edit bug report at https://bugs.php.net/bug.php?id=71758&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=71758&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=71758&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=71758&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=71758&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=71758&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=71758&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=71758&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=71758&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=71758&r=support
Expected behavior: https://bugs.php.net/fix.php?id=71758&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=71758&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=71758&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=71758&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71758&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=71758&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=71758&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=71758&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71758&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=71758&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=71758&r=mysqlcfg