代码之家  ›  专栏  ›  技术社区  ›  SoLoGHoST

在函数中插入参数时出现问题

  •  2
  • SoLoGHoST  · 技术社区  · 16 年前

    好的,我试着把参数放入一个函数中,这个函数的调用如下:

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

    下面是我需要放入的函数和参数:

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

    1. 要输出的错误
    2. 字符串值中记录的错误类型(“常规”、“关键”等)。

    下面是我得到的结果: 这是一个测试错误。ArraycriticalArray1

    我知道这个函数工作得很好,但它只给我返回的第一个参数。我拿着这个做什么不对 $parameters['params']

    编辑:函数如下:

    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 回复  |  直到 16 年前
        1
  •  2
  •   Tom Haigh    16 年前

    你可能想要 call_user_func_array() . 作为第二个参数传递的数组的每个项都将用作函数参数,例如:

    call_user_func_array( $parameters['function'], $parameters['params'] );
    
        2
  •  0
  •   Sangwon Park    16 年前

    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"));
    ?>
    
    推荐文章