Jump to content

No more pass by reference in 5.4


LLLLLLL

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.