I have got a couple of functions that manipulate data in an array, for example unset_data() you pass it the value and an unlimited amount of string args like:
unset_data( $my_array, "firstname", "password" );
and it can handle multi-dimentional arrays etc, quite simple.
But should this function use a reference to the array and change it directly?
Or should i return the new array with the values unset.
I can never decide whether a function should use reference or not,
Is there like, specific cases or examples when and when to not use them??
Thanks
I’d ask myself what the expected use case of the function is. Does the typical use case involve keeping the original data intact and deriving new data from it, or is the explicit use case of this function to modify data in place?
Say
md5would modify data in place, that would be pretty inconvenient, since I usually want to keep the original data intact. So I’d always have to do this:instead of:
That’s pretty ugly code, forced on you by the API of the function.
For
unsetthough, I don’t think the typical use case is for deriving new data:That seems pretty clunky as well as possibly a performance hit.