Масивите по дефолт по реферънс ли се предават?

https://stackoverflow.com/questions/2030906/are-arrays-in-php-passed-by-value-or-by-reference

Тhe array is passed by reference UNLESS it is modified within the method / function you’re calling. If you attempt to modify the array within the method / function, a copy of it is made first, and then only the copy is modified. This makes it seem as if the array is passed by value when in actual fact it isn’t.

Еven though you aren’t defining your function to accept $my_array by reference (by using the & character in the parameter definition), it still gets passed by reference (ie: you don’t waste memory with an unnecessary copy).

However if you modify the array, a copy of it is made first (which uses more memory but leaves your original array unaffected).

FYI – this is known as „lazy copy“ or „copy-on-write“.

Вашият коментар

Вашият имейл адрес няма да бъде публикуван. Задължителните полета са отбелязани с *