PHP passes array by value unless you declare function signature to take reference
yep, calling the function below will not have changed the array after the call
public function addMore(array $names) {
$names[] = 'another';
return;
}
..you have to declare the function signature to take a reference to the array, then the array will be changed after the call
public function addMore(array &$names) {
$names[] = 'another';
return;
}
Objects passed can be modified without the need to declare as references in the signature as they are passed by reference by default;
public function addMore(array $names) {
$names[] = 'another';
return;
}
..you have to declare the function signature to take a reference to the array, then the array will be changed after the call
public function addMore(array &$names) {
$names[] = 'another';
return;
}
Objects passed can be modified without the need to declare as references in the signature as they are passed by reference by default;
Comments
Post a Comment