Jump to content

Variable references


zq29

Recommended Posts

Like OOP, I understand the theory and practice of how they work - But how are they useful? What would be an every day use for variable references? I just don't see the obvious point in them. Why have two variables that reference the same allocation of memory?

Link to comment
Share on other sites

Well i can explain it better using C++ as a reference because in C++ you always pass by reference whenever you can because it take up less memory and the code execute a little bit faster.  Now let me try to give you an example in php where you would want to pass by reference.

 

The main reason to pass a variable by reference in php in my opinion is if you want to be able to modify that value directly.  For instance, in my framework's data_model class there is a static method called resolve_id where you pass it a field name and a value like:

<?php
$user_id = 'admin';
user_model::resolve_id('username', $user_id);
?>

after the second line of code now $user_id becomes the id of the user with the username of admin.  Now granted, can accomplish the same thing by doing

<?php
$user_id = 'admin';
$user_id = user_model::resolve_id('username', $user_id);
?>

and not pass the value by reference but to me the first way a just a little bit cleaner.

 

I can't think of an example where is would be impossible to do something without passing the value by reference, is seems to be more of a coding choice.  I would have to do some benchmarking to see if there is a speed difference.

Link to comment
Share on other sites

Like OOP, I understand the theory and practice of how they work - But how are they useful? What would be an every day use for variable references? I just don't see the obvious point in them. Why have two variables that reference the same allocation of memory?

 

You'd want two variables to reference the same memory location so that you can modify both at once, like in a function.  For example, shuffle() passes by reference (notice how you never assign the result of shuffle() to a variable).  Like Liquid Fire said, I'd have an easier time giving practical examples in C rather than PHP, because in C it's necessary sometimes, while in PHP it can always (at least, I can't think of any exceptions...) be accomplished using standard variable assignment.

Link to comment
Share on other sites

But why would the developers have chosen to have some in-built functions pass by reference, and others assign by variable? Like your example with shuffle(), why not with, say, array_reverse() ?

 

Give the example in C(++) if you need to, I don't understand the necessity to be language specific when explaining their usefulness...

Link to comment
Share on other sites

I wish I could tell you that.  I ask myself that every once in a while.  Kinda frustrating to be honest.  And the C or C++ examples actually have to do with directly manipulating things based on their memory location with pointers, which is not exactly the same as PHP references.  They function similarly, but they're by no means the same.  An example in C or C++ would mean nothing to a PHP programmer as PHP programmer don't need to worry about memory management and stuff.

Link to comment
Share on other sites

But why would the developers have chosen to have some in-built functions pass by reference, and others assign by variable? Like your example with shuffle(), why not with, say, array_reverse() ?

 

Give the example in C(++) if you need to, I don't understand the necessity to be language specific when explaining their usefulness...

 

The first question is a really good one and i don't think that is a real answer as the entire PHP language and plagued by stuff like that. for instance in str_repeat() the string to repeat is passed first but in str_replace() the string to replace in is passed last.  Another instance is the function name str_replace() and str_repeat() with underscores but then you have strpos() and substr() with out underscores.  The whole language has really no consistency which is probably the thing i hate most about php but it is something i have learn the live with.

 

As for the C++ example, DarkWater is right that it is more or a memory management and code speed issue(because passing a variable address it a lot easier that passing a variable itself) but with php you don't have to worry about that since variables are automatically passed by reference and the copy of the variable is only created when you try to modify a variable that is not manually passed by reference(I hope this did not confuse you more).

Link to comment
Share on other sites

SemiApoc, one good thing about references is that they're much less expensive than a new variable.  Let's say you're passing a 10MB string to a function, do you really need two copies of it?

 

Also, interesting side note, anytime a variable is assigned to another variable or it's passed to a function, it's a reference (not in the C[++] sense, since it works slightly differently.  It doesn't point to the memory location.  One of those weird PHP engine things ;p) until it's modified.  That's to help with the situation mentioned above.  For example, how often do people really need to copy $_GET or $_POST vars when they first assign them to a variable?

 

 

 

The best example I can think of for PHP is resources.  You wouldn't want to copy a file handle (impossible sometimes if locking occurs) or a socket link would you?  You wouldn't want to copy a passed DB object either would you (objects aren't copied by default when assigned to variables.  Another PHP thing.)?  Actually, now that I think about it, resources may always be references.  If so, that would be a good example in C[++].  You wouldn't want to copy a resource in C[++] for sure.  It could create major chaos.

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.