Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to lay out even simple functions using references

I have a function:

$query = "SELECT * from lol";
database_query( $query );

considering the $query will never be changed inside the database_query function, is it good practice to use a pointer to $query so the function doesn't need to assign more memory to a new iteration of the value passed in?

function database_query( &$query ){
    //do stuff that does not affect $query
}
like image 986
Jimmyt1988 Avatar asked Dec 12 '12 12:12

Jimmyt1988


1 Answers

No, don't do this. PHP will only create another copy of the string if the value of the non-pass-by-reference parameter is changed inside of the function ("copy on write"). There is no reason to give people reading your code the wrong impression of what your function is doing by making the parameter a reference.

Also, references are not pointers.

like image 66
Tim Cooper Avatar answered Oct 06 '22 01:10

Tim Cooper