Req #71758 [Opn->Wfx]: can't initialize inline a pass-by-references variable parameter

From: Date: Fri, 12 Nov 2021 13:48:30 +0000
Subject: Req #71758 [Opn->Wfx]: can't initialize inline a pass-by-references variable parameter
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237725@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71758&edit=1 ID: 71758 Updated by: nikic@php.net Reported by: vittorio dot zamparella at gmail dot com Summary: can't initialize inline a pass-by-references variable parameter -Status: Open +Status: Wont fix Type: Feature/Change Request Package: Scripting Engine problem PHP Version: 7.0.4 Block user comment: N Private report: N New Comment: I'll just go ahead and mark this as Won't Fix. We generally have no interest in making use of references more ergonomic, and rules around reference usage are going to become stricter rather than more lenient over time. Case in point, the sample code throws an Error exception in PHP 8.0. Use of references is discouraged unless unavoidable, and I'm really hard pressed to think of a legitimate use-case for reference where the ability to perform an inline assignment would be important. Previous Comments: ------------------------------------------------------------------------ [2021-09-22 16:40:49] cmb@php.net > Maybe I'm missing some symple syntactic sugar to work around > this […] No, you don't. However, I don't think this will be improved; after all, there are not that much reasons to use references at all, and in-out parameters might not be that common, either. ------------------------------------------------------------------------ [2016-03-09 15:36:39] vittorio dot zamparella at gmail dot com 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 this bug report at https://bugs.php.net/bug.php?id=71758&edit=1

« previous php.bugs (#237725) next »