PHP function - ignore some default options

Possible duplicate:
Any way to specify additional parameter values ​​in PHP?

just stumbled upon it.

If I have a function like this:

public function getSomething($orderBy='x', $direction = 'DESC', $limit=null){ //do something random } 

When calling a function, you can ignore the first two fields and leave them by default, but specify the third.

For instance:

 $random = $this->my_model->getSomething(USE_DEFAULT, USE_DEFAULT, 10); 

I know that I can pass the 1st and 2nd parameters, but they all ask if this is a kind of special keyword that just uses the default value.

Hope this makes sense. this is not a problem, just curious.

thank you for reading

+6
source share
4 answers

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()) { // ... } 
+11
source

Honestly, this becomes a problem when functions try to do too much. There is almost always the best design template when you see that a function grows to a few parameters (usually this is a poor guy who inherited the old code, and adding parameters is the fastest way to "complete the task").

An exceptional answer is best on your question, but look at the cyclomatic complexity: http://en.wikipedia.org/wiki/Cyclomatic_complexity

This is a good way to find out if your function does too much, which makes your question less problematic than it is possible now.

+1
source

I do not think PHP can do this. The best solution that I know of is outlined here: http://www.php.net/manual/en/functions.arguments.php#70511

0
source

You cannot ignore the options. But you can do something like this:

 public function getSomething($limit=null){ return $this->getSomething('x','DESC',$limit); } public function getSomething($orderBy='x', $direction = 'DESC', $limit=null){ ... } 

see ya

-2
source

Source: https://habr.com/ru/post/909862/


All Articles