php - Is it a good idea to create variables using references? -
example:
function create_pets(&$cats, &$dogs){ $dogs = get_dogs(); $cats = get_cats(); } so call like:
function foo(){ create_pets($cats, $dogs); // here use $cats , $dogs variables } i know assign new varible return value of 1 of getter functions, example. in situation there's more getter...
the answer says "it depends". in specific example, "create" function, code less obvious work , maintain, , it's idea avoid pattern.
but here's news, there's way of doing trying keeps things simple , compact while using no references:
function create_pets(){ return array(get_dogs(), get_cats()); } function foo(){ list($dogs, $cats) = create_pets(); //here use $cats , $dogs variables } as can see can return array , use the list language construct individual variables in single line. it's easier tell what's going on here, create_pets() returning new $cats , $dogs; previous method using references didn't make clear unless 1 inspected create_pets() directly.
you not find performance difference of using either method though, both work. you'll find writing code easy follow , work on goes long way.
Comments
Post a Comment