3

Ok so I have a function with 2 mandatory arguments and then it must have many optional arguments too.

function example($a,$b, $username, $email) {
    // code
}

My data for the optional arguments comes from an array

$x = array('joeblogs', '[email protected]');

How would i be able to parse these? bearing in mind that the function may be required to parse a different set of arguments each time.

An example is with CakePHP you can specify the action arguments that are required

5 Answers 5

5

Something like this?

$a = 'a';
$b = 'b';
$x = array('joeblogs', '[email protected]');

$args = array_merge(array($a, $b), $x);

call_user_func_array('example', $args);

See http://php.net/manual/en/function.call-user-func-array.php

Sign up to request clarification or add additional context in comments.

Comments

1

There are two approaches to optional arguments.

In the first, you specify all of the arguments like this:

function example($a, $b, $c=null, $d=null, $e=null)

Parameters $a and $b are required. The others are optional and are null if nothing is provided. This method requires that each of the optional parameters be specified in the indicated order. If you want to call the method using only $a, $b and $e you have to provide null values for $c and $d:

example($a, $b, null, null, $d);

The second method accepts an array as the third parameter. This array will be checked for keys and processed based on the keys found:

function example($a, $b, $c=array()) {

    $optionalParam1 = ( !empty( $c['param1'] ) ) : $c['param1'] ? null;
    $optionalParam2 = ( !empty( $c['param2'] ) ) : $c['param2'] ? null;

In this way, you can check for each key that may be provided. Null values will be provided for any key not populated.

Comments

0

Following shows syntax for optional parameters and default values

function example($a,$b, $username = '', $email = '') {

}

Another possibility is to pass an "optional values array"

function example($a,$b, $optional_values = array()) {
    if($optional_values[0] != '') { blah blah .... }
}

Comments

0

This solution is a merge of your sugestion and Jez's solution.

call_user_func_array(array($controller, $action), $getVars);

Where $controller is the instance of your controller, $action is the string to the action that you want to call, and $getVars is an array of parameters.

The first parameter of the call_user_func_array function is a callback. It's possible to define a method invocation as callback.

Here is a link to the documentation of PHP's callback: http://www.php.net/manual/pt_BR/language.pseudo-types.php#language.types.callback

Comments

0

To pass the array parameters to a function you can use call_user_func_array:

$args = array( 'foo', 'bar', 'joeblogs', '[email protected]' );
call_user_func_array( 'example', $args );

Or simple pass any number of parameters:

example( $a, $b, $username, $email );

To retrieve the parameters inside function use func_get_args:

function example() {
    $args = func_get_args();

    print_r( $args );

    // output:
    //  Array ( 
    //      [0] => foo 
    //      [1] => bar 
    //      [2] => joeblogs 
    //      [3] => [email protected] 
    //  )

}

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.