What is the best practice regarding passing data to functions - variables, or arrays(objects).
For example, I need user info for most functions. Should I pass full user
As a developer when you start out coding, you have a certain number of things a function will require in order for it to work, some may be optional too, so you set out to create a method signature similar to the one below.
function do_something($var1, $var2, $var3, $var4 = false, $var5 = '') {
//process business logic here
}
Now, say you implement and get more feedback on features and other improvements. If the features or improvements suggested require you to change the method signature to incorporate new variables and you use default values, you are in a bit of a spot.
So what is the best way to avoid such situations? In my opinion, if you use a function which accepts variables, it could easily scale to accept more parameters without breaking anything by accepting an array as a single parameter. The array would have to be built up either internally in the code somewhere or externally through the user.
So you could rewrite the above logic as below:
function do_something($args = array()) {
$myvar = (array_key_exists('mykey', $args)) ? $args['mykey'] : get_default_value('mykey');
//process business logic here
}