I have a function that returns an integer, however I would like to expand it to add a new param to it. With this param, however, the function would have to return an array.
I think it would be bad practice also to just copy-paste the entire function for 4-5 extra lines.
Every programming language lets you create blocks of code that, when called, perform tasks. Imagine a dog that does the same trick only when asked. Except you do not need dog treats to make your code perform. In programming, these code blocks are called functions.
If at all possible, I would call one function from within another.
function getSingle($arg)
{
// Do whatever it is your function should do...
return 1;
}
function getMultiple($args)
{
$out = array();
foreach ($args as $arg) {
$out[] = getSingle($arg);
}
return $out;
}
For the function you have in mind, it might not be possible to do this, but it may be a good option.
As an extra note, as the functions are related to one another, I would write them as class methods to "group" them together.
Take Users
for example; I might need a function to get a single user and another to get multiple users. It makes sense to collect these methods in a class:
class Users
{
public function getUser($id){}
public function getUsers(array $id = null){}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With