When we need to create a pass / call to the help function

I will always be confused as to whether to create pass / call by reference functions. It would be great if someone could explain exactly when I should use it and some realistic examples.

+3
source share
4 answers

A common reason for calling by reference (or pointers) in other languages ​​is to persist in space - but PHP is smart enough to implement copy-on-write for arguments declared to be passed by value (copies). There are also some hidden semantic oddities - although PHP5 introduced the practice of always passing objects by reference, array values ​​are always stored as links, call_user_func () always calls by value - never by reference (because it is itself a function - not a construct).

But this is complementary to the original question asked.

, (), , . , , , . .

+3

PHP 5 .

- . , , sort. , , , ( ).

, ( ).

+2

php4 . , , . :

function foo(&$arr)
{
  echo $arr['value'];
}
$arr = new array();
foo($arr);

, . php5 ( , int), , .

0

, , ,

 
$var = modify($var); 
function modify($var)
{
return $var.'ret'; 
}

, great.

In addition, when working with large variables, and especially arrays, it is useful to pass by reference where possible. This helps save memory.

Usually I pass a link when working with arrays, since I usually return to the modified array in the original array.

0
source

All Articles