Req #70296 [Wfx]: pass by reference: switch back to call time

From: Date: Wed, 19 Aug 2015 00:40:25 +0000
Subject: Req #70296 [Wfx]: pass by reference: switch back to call time
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-195318@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70296&edit=1 ID: 70296 Updated by: requinix@php.net Reported by: o dot rijkers at gmail dot com Summary: pass by reference: switch back to call time Status: Wont fix Type: Feature/Change Request Package: Unknown/Other Function PHP Version: Irrelevant Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2015-08-19 00:32:45] 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-18 23:41:41] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70296&edit=1

« previous php.bugs (#195318) next »