Passing variables by reference
Hello, dear community.
Small question to experts. Often in their projects use php variable references. For example:
or another option:
I would like to know if writing is the meaning and impact of that performance or not? As I understand it, php, the idea is to pass the address of a string or array, and the contents of the variable in the memory copy will not. And that's how it actually happens, I have no idea... Maybe in php it includes everything, and he himself with some of its internal algorithms and makes?
It is not difficult, of course, to add everywhere "&", but whether it is at least is negligible impact on the loading time of a page?
Answered: 24-12-2012 в 01:58:30
Clear) Thanks! - 24-12-2012 в 12:15:11
You should not think about performance at this stage. Productivity issues need to be solved when problems arise. Think about the meaning of the parameters.
Transfer parameter by reference means that the parameter can be modified by the called procedure. Is that what you want? If the called procedure should not in any case to change the value of the transition variable, no need to pass it by reference. Also, consider that in as a reference parameter, you cannot pass an expression of type
Reference means "the option has the right to be modified by the called function". Whoever will read the code after you, will be forced to assume that such changes are possible-or he will have to get used to your unusual style of programming.
You have to think about readability and phonememory code and not look for meager winnings. Because a tiny win will result in challenges in maintaining and scaling the project.
Certainly be passed by reference or by value have any effect on the loading time of the page, but it all depends on the version. For example, in some cases in C++ before, it was recommended to pass parameters by reference, now -- by value.
(Perhaps a special case is the transmission of large amounts: consult with experts in the language!)
Obligatory quote from Knut:
Answered: 23-12-2012 в 17:50:51
Thanks for the reply. I agree that at the development stage, the optimization may be unnecessary, if only because the code at all, then it may be turned inside out. But I do it anyway, sooner or later will ask this question functions and classes that are everywhere, and to spend extra resources you do not want... - 23-12-2012 в 19:49:59
@Dobby007: Please! At the time of optimization, you will find those 3% and optimize them. But just do not start before time, the bottleneck is not installed yet! - 23-12-2012 в 20:16:01
@Dobby007, you first think about how much time and resources are wasted, for example on database connection, work with templates or similar risky operations. Compared to this transfer of parameters is sophomoric :) - 24-12-2012 в 12:54:27