views:

37

answers:

2

Ok, I am trying to place parameters into a function that is called like so:

$parameters['function']($parameters['params']);

Here is the function and the parameters that I need to be placed into this:

$parameters['function'] = 'test_error';
$parameters['params'] = array(0 => $txt['sometext'], 1 => 'critical', 2 => true);

The test_error function takes 3 parameters:

  1. The error to output
  2. The type of error noted within a string value ('general', 'critical', etc. etc.).
  3. Whether it should be echoed or not.

Here is the output I get: Here is a test error.ArraycriticalArray1

I know this function works perfect, but it only gives me the 1st parameter returned from it. What am I doing wrong here with the $parameters['params']??

EDIT: Here is the function:

function test_error($type = 'error', $error_type = 'general', $echo = true)
{
    global $txt;

    // Build an array of all possible types.
    $valid_types = array(
        'not_installed' => $type == 'not_installed' ? 1 : 0,
        'not_allowed' => $type == 'not_allowed' ? 1 : 0,
        'no_language' => $type == 'no_language' ? 1 : 0,
        'query_error' => $type == 'query_error' ? 1 : 0,
        'empty' => $type == 'empty' ? 1 : 0,
        'error' => $type == 'error' ? 1 : 0,
    );

    $error_html = $error_type == 'critical' ? array('<p class="error">', '</p>') : array('', '');
    $error_string = !empty($valid_types[$type]) ? $txt['dp_module_' . $type] : $type;

    // Should it be echoed?
    if ($echo)
        echo implode($error_string, $error_html);

    // Don't need this anymore!
    unset($valid_types);
}
+2  A: 

You probably want call_user_func_array(). Each item of the array passed as the 2nd argument will be used as a function argument, for example:

call_user_func_array( $parameters['function'], $parameters['params'] );
Tom Haigh
hmmm, so there's no way to use this approach `$parameters['function']($parameters['params']);`?? The thing is I'd like it to call the function with this approach, cause it does it fine already for non-errors, so I'd rather not have to change it, as it would be a ton more work.
SoLoGHoST
You would have to do `$parameters['function']($parameters['params'][0], $parameters['params'][1], $parameters['params'][2]);`, or modify the function.
Tom Haigh
Is there a way to set `$parameters['params'] = "all parameters in here";` ??? Would doing this work? If so, how exactly should it be done while still use the same function setup.
SoLoGHoST
@SoLoGHoST: no. But I don't see why you would want to; `call_user_func_array()` works exactly like this
Tom Haigh
Ok, Thank You Very Much :)
SoLoGHoST
A: 

See below.

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

<?php
function foobar($arg, $arg2) {
    echo __FUNCTION__, " got $arg and $arg2\n";
}
class foo {
    function bar($arg, $arg2) {
        echo __METHOD__, " got $arg and $arg2\n";
    }
}


// Call the foobar() function with 2 arguments
call_user_func_array("foobar", array("one", "two"));

// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
?>
tastyone