1

While I've taught myself a great deal, today I have been struggling with an unexpected behaviour of calling parameters in a function.

Here's an example function:

public function example($foo = null, $bar = null) {
    if ($foo) {
        // Do something
    }

    if ($bar) {
        // Do Something
    }
}

Now if I write a call that looks like the following, I was expecting to call $bar (the second parameter) instantly, which is not the case - and it makes sense to me why.

$this->example($bar);

Is there a way to just call the second parameter without using the following?

$this->example(null, $bar);
6
  • 1
    Since you defined it as a parameter you need to fill it in that order: $this->example(null, $bar);. Otherwise $bar will be assigned as the first parameter. Commented Feb 27, 2017 at 0:32
  • 3
    And in case there is confusion, the names are independent, ie. you can call $this->example(null, $b); or $this->example($a, $b); or $this->example($blah); Commented Feb 27, 2017 at 0:35
  • Thanks for the answer and for correcting my grammer mistake, Darren. My problem is that sometimes I enhance functions later in development and I'm trying to find a way to specifically map parameters of the function, so I don't have to edit / check all calls in my project. Is that something you can do in php? Commented Feb 27, 2017 at 0:35
  • 1
    Not primarily no. You'd probably have to hook into something like func_get_args() and pass an array to your function, like this Example. (Not the best practice at all though. I'd avoid this personally.) Commented Feb 27, 2017 at 0:43
  • That makes sense. I had seen this in the past and wondered why someone took that route. Thanks, I'll try to avoid it as well, but at least I know this way exists. Commented Feb 27, 2017 at 1:04

1 Answer 1

1

Well, like was suggested by Darren and his example, you can use function func_get_args, but there isn't much difference, so you should use which one fits you. Using func_get_args, though doesn't seem too comfortable, so what is the reason not to use direct params? Pick any value you want to use as indicator (null, false, 'barambam') and do something like this:

const ARGFALSE = 29292929299;
function foobar($f, $o, $o, $b, $a, $r){
if (!$f)// equivalent to $f != false
echo 'First arg should not be used.';
If (!$o == ARGFALSE)echo 'Second is taging along with the first';
foobar(false, ARGFALSE);

Still, in some (or most) cases you may do it backwards (another example):

function getUsers($type, $limit = false) { // or your default value. It can be almost anything.
$query = 'Select from users where type = ?';
If($limit){
$query .= ' limit ?';
return $result = Db::run($query, [$type, $limit]);
}else{
return $result = Db::run($query, [$type]);
}
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.