Code Lover
Code Lover

Reputation: 8348

PHP: Function with user callback

First time I am trying to make function to overload the main function. So need some guidance to understand and write in better and future proof way.

I am creating this function for navigation and to allow developers to overload/override main function.

Main Function

I am not sure if I have written this function in most efficient way. I would appreciate your guide to make it better.

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;
}

So here the final result with or without callback I want is multidimensional array as below example

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

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

)

I am able to achieve above result, however not so sure if it is correct and efficient way and secure enough.

So here are some examples using what I have done

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

// this function result
main_func($args);

Result

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

)

Using callback

// 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

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

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

)

Seems result is fine and what I am looking for. However, I want developer allow to pass function name (string) as a callback function instead of anonymous function and that is I don't know how to do.

Something like this

// 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');

So here what finally I want

  1. Make main function more efficient and future proof
  2. Allow passing function name as a callback instead of anonymous function
  3. Any other tip to make it even better

Million thanks

Upvotes: 1

Views: 76

Answers (1)

mmm
mmm

Reputation: 1148

if you want to allow tu callback function to completly modify the array, you can do that :

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

and in the call :

main_func($args, function ($args) {

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

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

other example where the array is reset

main_func($args, function($user_args) {

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

    return $user_args;
});

.


test with function names

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);

Upvotes: 1

Related Questions