php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #71758 can't initialize inline a pass-by-references variable parameter
Submitted: 2016-03-09 15:36 UTC Modified: 2021-09-22 16:40 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: vittorio dot zamparella at gmail dot com Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 7.0.4 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: vittorio dot zamparella at gmail dot com
New email:
PHP Version: OS:

 

 [2016-03-09 15:36 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-09-22 16:40 UTC] cmb@php.net
-Package: Compile Warning +Package: Scripting Engine problem
 [2021-09-22 16:40 UTC] 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.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Sep 27 21:03:38 2021 UTC