PHP:用户回调函数

时间:2015-07-28 07:02:34

标签: php callback

我第一次尝试使函数重载主函数。因此,需要一些指导,以更好和未来的方式来理解和写作。

我正在创建此导航功能,并允许开发人员重载/覆盖主要功能。

主要功能

我不确定我是否以最有效的方式编写了此功能。我很感谢你的导游让它变得更好。

function main_func( $args, $callback = NULL )
{
$result[] = $args;
/*
 * This is just for example
 * So by calling user function below,
 * I want to allow developer to add additional functionality
 * Or can override condition before function returns the value
 */
if ( array_key_exists('page', $args) ) {
    // some stuffs based on condition
}

/*
 * I am not so sure about the placement of the call_user_func
 * As I believe this might be okay because I want to allow
 * developers to override above condition and manipulate $args
 * before this function returns the result
 */

/*
 * I am not so sure but probably something like this
 * $args[] = array_merge($args, call_user_func($callback, func_get_arg(func_num_args())));
 */
if ( is_callable($callback) ) {
    $result[] = call_user_func($callback, $args);
}

/*
 * Here I want to merge two array items
 * 1. Function's $args argument
 * 2. User callback function $args
 *
 * This means user will be able to add own array item within
 * the nested array item for $args
 *
 * So final result I am looking is
 *
 *   array(
 *      $args = array(), array(), ... // function args array items
 *      $args = array(), array(), ... // user callback function args array item
 *   );
 */

/*
 * So here I get combined array with
 * user own callbck function logic and
 * custom array items within the array
 */
return $result;
}

所以这里有或没有我想要的回调的最终结果是多维数组,如下例

Array
(
    [0] => Array
        (
            [auth] => 
            [url] => user-photos
            [label] => Social Photos
            [selected] => 1
        )

    [1] => Array
        (
            [auth] => 
            [url] => user-settings
            [label] => Social Settings
            [selected] => 0
        )

)

我能够达到上述效果,但不能确定它是否正确有效且足够安全。

以下是使用我所做的一些例子

// function args
$args = array(
    'auth'     => FALSE,
    'url'      => 'user-photos',
    'label'    => 'Social Photos',
    'selected' => 1
);

// this function result
main_func($args);

结果

// result
Array
(
    [0] => Array
        (
            [auth] => 
            [url] => user-photos
            [label] => Social Photos
            [selected] => 1
        )

)

使用回调

// callback args
$user_args = array(
    'auth'     => FALSE,
    'url'      => 'user-settings',
    'label'    => 'Social Settings',
    'selected' => 0
);

// function with user callback
main_func($args, function(){
    $user_args = array(
        'auth'     => FALSE,
        'url'      => 'user-settings',
        'label'    => 'Social Settings',
        'selected' => 0
    );
    return $user_args;
});

结果

// result
Array
(
    [0] => Array
        (
            [auth] => 
            [url] => user-photos
            [label] => Social Photos
            [selected] => 1
        )

    [1] => Array
        (
            [auth] => 
            [url] => user-settings
            [label] => Social Settings
            [selected] => 0
        )

)

似乎结果很好,我正在寻找什么。但是,我希望开发人员允许将函数名称(字符串)作为回调函数而不是匿名函数传递,这是我不知道该怎么做。

像这样的东西

// user callback function
function user_func($user_args)
{
    // do my stuffs

    if (some condition) {
        // do all stuffs
    }

    return stuffs;
}

// Now callbck user function by function name instead of anonymous function
main_func($args, 'user_func');

所以最后我想要

  1. 使主要功能更有效,面向未来
  2. 允许将函数名称作为回调而不是匿名函数传递
  3. 使其更好的任何其他提示
  4. 百万谢谢

1 个答案:

答案 0 :(得分:1)

如果你想允许tu回调函数来完全修改数组,你可以这样做:

$args = call_user_func($callback, $args);

并且在电话中:

main_func($args, function ($args) {

    // examples
    $args[0]["selected"] = 0;
    $args[] = array("toto");

    // retun the modified array
    return $args;
});

重置数组的其他示例

main_func($args, function($user_args) {

    $user_args = array(
        'auth'     => FALSE,
        'url'      => 'user-settings',
        'label'    => 'Social Settings',
        'selected' => 0
    );

    return $user_args;
});

使用函数名称

进行测试
function main_func($args, $callback = NULL) {

    if (is_callable($callback)) {
        $args = call_user_func($callback, $args);
    }

    return $args;
}

$args_test = array(
    array(
        "auth" => FALSE,
        "url" => "user-photos",
        "label" => "Social Photos",
        "selected" => 1,
    ),
);


function test_callback($t) {

    $t[0]["selected"] = 0;

    $t[] = array("toto");

    return $t;
}


// test with function name

$test1 = main_func($args_test, "test_callback");

var_dump($test1);


// test with anonymous function

$test2 = main_func($args_test, function ($a) {
    return test_callback($a);
});

var_dump($test2);


// test with no callback

$test3 = main_func($args_test);

var_dump($test3);