teynon Posted October 16, 2009 Share Posted October 16, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/177956-questions-about-references/ Share on other sites More sharing options...
chronister Posted October 16, 2009 Share Posted October 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/177956-questions-about-references/#findComment-938321 Share on other sites More sharing options...
mikesta707 Posted October 16, 2009 Share Posted October 16, 2009 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++ Quote Link to comment https://forums.phpfreaks.com/topic/177956-questions-about-references/#findComment-938332 Share on other sites More sharing options...
teynon Posted October 16, 2009 Author Share Posted October 16, 2009 Ok, I see what you guys are saying, but why wouldn't you just use a class then and use the direct reference with the internal functions IE $myClassObject->variable++; ? Quote Link to comment https://forums.phpfreaks.com/topic/177956-questions-about-references/#findComment-938340 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.