LLLLLLL Posted September 18, 2012 Share Posted September 18, 2012 If this was covered elsewhere on the forums, I apologize but search isn't working at all. So I'll ask potentially again... function some_func( &$var ) { $var++; } $var = 1; some_func( $var ); // var is now 2 Now I see that 5.4 has removed passing by reference? This is a little crazy, frankly, since all other languages have this feature and developers need it. Regardless, what is considered the standard way of writing this type of function now that it's no longer possible to use my example code here? Quote Link to comment https://forums.phpfreaks.com/topic/268487-no-more-pass-by-reference-in-54/ Share on other sites More sharing options...
trq Posted September 18, 2012 Share Posted September 18, 2012 Passing by reference has been deprecated since 5.3, that is over 3 years ago, it's not like this has snuck up on anyone. I suggest writting your code in a way that doesn't break the encapsulation that functions provide. function some_func($var) { return $var++; } $var = 1; $var = some_func($var); // var is now 2 Quote Link to comment https://forums.phpfreaks.com/topic/268487-no-more-pass-by-reference-in-54/#findComment-1378784 Share on other sites More sharing options...
LLLLLLL Posted September 18, 2012 Author Share Posted September 18, 2012 But what if I can't have that function return a variable? Quote Link to comment https://forums.phpfreaks.com/topic/268487-no-more-pass-by-reference-in-54/#findComment-1378785 Share on other sites More sharing options...
requinix Posted September 18, 2012 Share Posted September 18, 2012 Call-time pass-by-reference is deprecated. That means you can't some_func(&$var); Regular pass-by-reference, like your code demonstrates, is still perfectly fine. Quote Link to comment https://forums.phpfreaks.com/topic/268487-no-more-pass-by-reference-in-54/#findComment-1378789 Share on other sites More sharing options...
LLLLLLL Posted September 18, 2012 Author Share Posted September 18, 2012 OK. I think maybe some server administrator just doesn't know what he's talking about, then. I don't have any call-time references (I assume this only means where & is in front of the calling function parameter, rather than only in the function declaration). I think this server admin doesn't know much about PHP. Quote Link to comment https://forums.phpfreaks.com/topic/268487-no-more-pass-by-reference-in-54/#findComment-1378792 Share on other sites More sharing options...
trq Posted September 18, 2012 Share Posted September 18, 2012 I think maybe some server administrator just doesn't know what he's talking about, then. If that is aimed at me, you should read my reply again. I didn't say anything about references except that the change you have recently stumbled upon has been deprecated for a long time. I merely suggested you rewrite your code in a way that doesn't break encapsulation. I still stand by that. Functions that alter variables within the global namespace tend to lead to very flaky applications. Quote Link to comment https://forums.phpfreaks.com/topic/268487-no-more-pass-by-reference-in-54/#findComment-1378812 Share on other sites More sharing options...
LLLLLLL Posted September 18, 2012 Author Share Posted September 18, 2012 No, it was not aimed at you. It was aimed at the idiot who told me there was no passing by reference who made me start the thread. He sent me to a couple sites that made things unclear. I shouldn't have posted. Also, I disagree with your premise, however. Every language offers this feature and it's there for a reason. There are times when you need to modify a variable in a function. Quote Link to comment https://forums.phpfreaks.com/topic/268487-no-more-pass-by-reference-in-54/#findComment-1378842 Share on other sites More sharing options...
RussellReal Posted September 18, 2012 Share Posted September 18, 2012 No, it was not aimed at you. It was aimed at the idiot who told me there was no passing by reference who made me start the thread. He sent me to a couple sites that made things unclear. I shouldn't have posted. Also, I disagree with your premise, however. Every language offers this feature and it's there for a reason. There are times when you need to modify a variable in a function. php itself has functions which do this aswell, stream_select to name one Quote Link to comment https://forums.phpfreaks.com/topic/268487-no-more-pass-by-reference-in-54/#findComment-1378846 Share on other sites More sharing options...
trq Posted September 18, 2012 Share Posted September 18, 2012 Also, I disagree with your premise, however. Every language offers this feature and it's there for a reason. There are times when you need to modify a variable in a function. I am yet to find that need and would avoid doing so at pretty much any cost given the choice. Quote Link to comment https://forums.phpfreaks.com/topic/268487-no-more-pass-by-reference-in-54/#findComment-1378847 Share on other sites More sharing options...
Barand Posted September 18, 2012 Share Posted September 18, 2012 Does deprecated pass-by-ref apply to objects too? Quote Link to comment https://forums.phpfreaks.com/topic/268487-no-more-pass-by-reference-in-54/#findComment-1378874 Share on other sites More sharing options...
Barand Posted September 18, 2012 Share Posted September 18, 2012 Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. As of PHP 5.3.0, you will get a warning saying that "call-time pass-by-reference" is deprecated when you use & in foo(&$a);. And as of PHP 5.4.0, call-time pass-by-reference was removed, so using it will raise a fatal error. AFAIK this does not stop by ref in the function definition, only in the call. Quote Link to comment https://forums.phpfreaks.com/topic/268487-no-more-pass-by-reference-in-54/#findComment-1378877 Share on other sites More sharing options...
requinix Posted September 20, 2012 Share Posted September 20, 2012 Does deprecated pass-by-ref apply to objects too? Pass-by-reference works on variables, not their values. The variable itself is what's being passed around. Think of it as "pass-by-variable". If you're thinking of the PHP 4.x behavior where objects weren't themselves references (and thus passed-by-value, ie, copied) then yes, that's deprecated since the object model rewrite in 5.0: you don't need pass-by-reference to keep object references. Quote Link to comment https://forums.phpfreaks.com/topic/268487-no-more-pass-by-reference-in-54/#findComment-1379686 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.