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
- Make main function more efficient and future proof
- Allow passing function name as a callback instead of anonymous function
- Any other tip to make it even better