KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
Occasionally I'll write a PHP function with a single input, an associative array containing all of that function's inputs. This has benefits such as not having to remember the correct order of inputs, but I've also noticed it makes implementing changes to large codebases much easier; when I need to add another variable, and that variable has to pass through 4 or 5 pre-existing functions, it's much easier when I can just stick it in the array and pass it along. My question is, is there a downside to doing this? I rarely see functions written this way in examples, or in open source I use, which leads me to believe there is probably a downside. If there isn't a reason not to do it then why not write all functions this way? UPDATE Thanks for all your answers. It looks like two major problems stand out: Code readability - impossible to tell what variables are going into a function and what they're for Variable creep - Could wind up with massive arrays bouncing from one function to the next; one should not pass parameters to functions that don't require them Which are both great points I did not think about. It seems the general gist as well is that code where this is an issue, should probably be converted to a class. Unfortunately in this specific project such a refactoring is beyond the scope, but I also think Bill Karwin's solution is good - pass an array of optional variables
Tags (comma-separated)
Save Edits
Cancel