Jump to content

Questions about references


teynon

Recommended Posts

I'm sitting in Iraq and have some time to learn more about the different programming languages I have come to learn. This is really a question more than a coding problem. Perhaps you guys will get all upset about what I am asking and what not, but whatever...

 

Basically my question is, why would I ever use a reference IE function blah(&$var).

 

The only thing I could possibly see this as being useful is if I wanted a function to modify and return more than one variable. What else am I missing? Perhaps I am not understanding references correctly?

Link to comment
Share on other sites

From my understanding it is also a performance thing. If you use a reference to an object then it references that original object or variable in memory rather than creating a new instance of it.

 

Does not sound like a huge thing, but if you have hundreds of thousands of users hitting a side per day or even per hour, then every bit of performance that can be gained is a good thing.

 

I am sure there are other reasons, but that is the main one I know of.

Link to comment
Share on other sites

as was mentioned, there are performance issues, as well as issues if you want to update more than one variable.

 

to explain the performance may, so you have a huge data set that you want to pass into a function, and change. you have two choices, create a returning function that takes the data set as a parameter, or pass the data set by reference. Not only do you avoid making a copy of a huge data set (which if many people are doing, will severely slow down your server) but you don't have to return anything (a small point, I know)

 

using pass by reference with arrays can really help save a lot of your servers processing power. also, if, say, you want to update multiple variables passed in, without pass by reference you would have to return an array, and use the list function. What happens if based on some criteria, the function only wants to change 2 variables, or 1, or 4. well with pass by reference you don't have to worry. with the return array->list function method, you do have to worry.

 

 

but you really get a good appreciation of pass by reference if you were to code in, say, C/C++

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.