Passing variables by reference

The question was asked: 49 years 8 months ago   views: 3

Hello, dear community.

Small question to experts. Often in their projects use php variable references. For example:

class zzz { public function __construct(&$a, &$b, &$c){ $this->a = &$a; $this->b = &$b; $this->c = &$c; } } class yyy { ... public function init(){ $my1 = Array(Array(...), Array(...)); $my2 = 'Very long string'; $my3 = Array('line 1', 'line 2'); zzz new($my1, $my2, $my3); } }

or another option:

class zzz { public function __construct($cls){ $this->a = &$cls->my1; $this->b = &$cls->my2; $this->c = &$cls->my3; } } class yyy { public $my1 = Array(Array(...), Array(...)); public $my2 = 'Very long string'; public $my3 = Array('line 1', 'line 2'); ... public function init(){ zzz new($this); } }

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?

Asked: 01-01-1970 в 03:00:00

Answers   2


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 $n + 1.

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:

Programmers spend a huge amount of time thinking about (or fearing about) the speed of non-critical sections of their programs. These attempts to achieve efficiency have severely negative influence, when the consideration includes debugging and support of the program. We have to forget about small optimizations, say, 97% of the time: premature optimization is the source of all problems. However, we should not lose the opportunity to improve critical 3%.

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
@klopp, I just always wondered how much resources is spent on the transfer :) now I know) -
24-12-2012 в 13:24:37