Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gianmarx's avatar

optional parameters (PHP 8.0)

i wrote this function:

private function setter(Father $father = null, Children $children = null, array $params): Children {
    //logic ...
    ...
    ...
    ...
    return $children;
}

when I go to invoke the function I would like to make the first parameter optional a few times and the second optional at other times. if I try to invoke the function like this:

				$this->setter($children, $params);

give me following error:

Argument #2 ($children) must be of type ?src\bubu\test\Models\Children, array given, called in /home/vagrant/code/app/src/bubu/test/Repository/ChildrenRepository.php on line 55

0 likes
1 reply
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

You can use named parameters

$this->setter(children: $children, params: $params);
1 like

Please or to participate in this conversation.