php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70296 pass by reference: switch back to call time
Submitted: 2015-08-18 23:41 UTC Modified: 2015-08-19 00:40 UTC
From: o dot rijkers at gmail dot com Assigned:
Status: Wont fix Package: Unknown/Other Function
PHP Version: Irrelevant 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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: o dot rijkers at gmail dot com
New email:
PHP Version: OS:

 

 [2015-08-18 23:41 UTC] o dot rijkers at gmail dot com
Description:
------------
In the latest version the pass by reference is moved to the function declaration:

function myFunct(&$arg) {...}

This results in several limitations:
1. the $arg argument can only be a call by reference.
   The pass by reference requirement may be dependent of the location where
   the function is called (I just run into this, hence this request), i.e.
   sometimes I just want to call myFunc($a); and I don't want $a to be changed.
2. it is not possible to pass an expression or literal as the first argument.
3. it is not possible to define a default value (other than null) and omit the
   argument when calling the function, e.g. myFunc();

It should never be the responsibility of a function to enforce a pass by reference. At best it could provide the option of a pass by reference argument. This greatly improves the flexibility, especially for library developers.


Test script:
---------------
function myFunc(&$arg = 1) {}
function yourFunc($arg) {}

myFunc($a);    // no pass by reference as it is not desired
myFunc(3);     // no pass by reference as it is not desired
myFunc();      // no pass by reference as it is not desired
myfunc(&$a);   // pass by reference as it is required by the caller and
               // the option of pass by reference is provided by the function

yourFunc(&$a); // no pass by reference as this option is not provided by the
               // function declaration


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-08-19 00:32 UTC] rasmus@php.net
-Status: Open +Status: Wont fix
 [2015-08-19 00:32 UTC] rasmus@php.net
Latest version? This was changed in PHP 5.3.0 in 2009. There are a number of reasons why it needs to be this way, sorry.
 [2015-08-19 00:40 UTC] requinix@php.net
Rasmus beat me to it.

When a function defines a certain signature, being a combination of the parameters, their types, whether they are by-reference or not, and their default values, you need to obey the signature when you call it. That signature forms a contract that you, the developer, need to obey. If a function says a parameter needs to be passed by reference then there is a reason for it and you need to pass your argument by reference. You may not decide that you want to call the function in a different way than it was intended.

Meanwhile if the developer who created the function is abusing the concept of references, that's a different problem.

> 1. the $arg argument can only be a call by reference.
Right. Because the function says that argument has to be by reference.

> The pass by reference requirement may be dependent of the location where
> the function is called (I just run into this, hence this request), i.e.
> sometimes I just want to call myFunc($a); and I don't want $a to be changed
The function clearly intends to modify the variable - or at least have the capability to do so. If you don't want your $a to be modified then make a copy and pass that to the function instead.

> 2. it is not possible to pass an expression or literal as the first argument.
Correct. If the function wants a variable by reference then it means to modify the variable and so passing a non-variable does not make sense.

> 3. it is not possible to define a default value (other than null) and omit the
> argument when calling the function, e.g. myFunc();
Sure it is. https://3v4l.org/eQ6e4


See also bug #70278 which someone made just a couple days ago.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 11:01:34 2025 UTC