You need to do it yourself. You can use null
to indicate that the default value should be used:
public function getSomething($orderBy = null, $direction = null, $limit = null) { // fallbacks if ($orderBy === null) $orderBy = 'x'; if ($direction === null) $direction = 'DESC'; // do something random }
Then pass null
on invocation to indicate that you want to use the default values:
$random = $this->my_model->getSomething(null, null, 10);
Another possible solution that I sometimes use is an additional parameter at the very end of the parameter list, containing all optional parameters:
public function foo($options = array()) { // merge with defaults $options = array_merge(array( 'orderBy' => 'x', 'direction' => 'DESC', 'limit' => null ), $options); // do stuff }
Thus, you do not need to specify all optional arguments. array_merge()
ensures that you are always dealing with a full range of options. You would use it as follows:
$random = $this->my_model->foo(array('limit' => 10));
It seems that in this particular case there is no required parameter, but if you need it, just add it before the extra ones:
public function foo($someRequiredParameter, $someOtherRequiredParameter, $options = array()) {
source share