0

Currently I am using :

call_user_func(
    $func,
        $_POST['a'],$_POST['b'],$_POST['c'],$_POST['d'],$_POST['e']
);

However, as things grow in the script to call. The pressing need for something like:

call_user_func_array(
    $func,
       $array("_POST", $_POST)
);

Is more apparent. The thing overall with this is then I will need to create a new array I believe. Because in my script every $_POST is pushed to $func, besides $_POST[headers]. Though when I try the above it says :

Function name must be a string.

Maybe I just don't know how to work this in correctly. And could use some guidance. Thank you.

A example of how its all called is :

function somefunc($a,$b,$c,$d,$e)

Where $func is being set to one of the function, in example somefunc. Then all data besides headers is being pushed to that function.

In the way I have it now using call_user_func it is functioning properly. Though trying to convert over so that I do not always have to add to it, and that its set dynamically using a array.

Esoterica
  • 151
  • 1
  • 9

2 Answers2

1

It's less complicated than you're making it.

Here's an example of a working call to call_user_func_array():

<?php

function somefunc($a, $b, $c)
{
        echo "a = $a\n";
        echo "b = $b\n";
        echo "c = $c\n";
}

$post = array('x'=>1, 'y'=>2, 'z'=>3);

$func = 'somefunc';
call_user_func_array($func, $post);

The function takes two arguments:

  • The first is a callable which can simply be a name of a function.
  • The second is an array. The values of the array are assigned to the parameters of the function, in the same order. The keys of the array are ignored.

Your expression $array("_POST", $_POST) is not correct. It would call a function whose name is in the variable $array. I think you're confusing this with the callable syntax for objects.


You reference mysqli in your question title and tags, but you don't mention anything to do with mysqli in your question.

The most common use of call_user_func_array() with mysqli is when you want to call the function mysqli_stmt_bind_param() with a variable number of arguments, when you're trying to make a generic function to prepare any SQL statement and bind any number of parameters to it.

I've posted answers for this scenario a few times in the past:

Community
  • 1
  • 1
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • The function is being used along with Mysqli. In the example I posted data is being passed into the script via $_POST .. it then assigns each $_POST so that it can be passed into call_user_func. Your example is longer than what I did with call_user_func. I was wanting to design a dynamic method to extracting POST keys and values and push to call_user_func_array. To that extent & some thing you stated here I have had some success. The ones existing past being dynamically assigned were to be $func as its explicitely called & headers. – Esoterica Nov 16 '13 at 20:39
  • If I was to follow your example it makes the call more static than dynamic. In my new design I am trying to do it will allow numerous other scripts to interact with this one. And this one would not have any set keys or values. Which all would pass into it. The outside scripts would send their keys with values. And then this is to dynamically adjust and push to call_user_func_array. I have got that function working as it should. Its just now syncing call_user_func_array to it. While some might say post the code. I think its a basic idea to say: – Esoterica Nov 16 '13 at 20:45
  • Collect all $_POST key/values and assign them into call_user_func_array. This means only at run time does it know the keys. At run time does it know the value. They are not preset. Currently I have it preset with them and using call_user_func. I'm trying to make it dynamic. So the script just uses whatever data is passed into it. Then passes it to the desired function. Not all functions are working with Mysqli only a few. I'll post my work when I can work a bit more on my idea. – Esoterica Nov 16 '13 at 20:47
1

The answer was actually simple. While PHP could do array_keys($_POST) and array_values($POST). It would try to include the key/values to headers,func. Which in my case was not needed. So instead of collecting the key/values and excluding those 3. Then splitting them up again. I pushed the results to 2 arrays instead named $postk and $postv.

My changes from old to new:

OLD:

call_user_func(
    $func,
        $_POST['a'],$_POST['b'],$_POST['c'],$_POST['d']
);

NEW:

$i=0;
    foreach(array_diff_key(
           $_POST, array_flip(array('headers', 'func'))) as $k => $v){
        $postk[$i]=$k;
        $postv[$i]=$v;
        ++$i;
    }

call_user_func_array($func,$postv);

In this way it allowed me to push the arrays $postk/$postv of any length into functions. Which was set by POST besides headers,table,func.

EDIT I did want to add that the original help code thats all over the internet. It was that help text and where it was located with other responses which ultimately gave me the desired result.

Esoterica
  • 151
  • 1
  • 9
  • You might also like to read about [array_intersect_key()](http://php.net/array_intersect_key) if you want to restrict the keys of $_POST to a subset of keys. – Bill Karwin Nov 18 '13 at 19:33
  • @BillKarwin Thank you kindly for the reply. I did not see your reply until now. I am still learning about all the things available to use, and how to best use them. So thanks for pointing array_intersect_key() out. Will actually change this bit of code tomorrow and use it. – Esoterica Nov 21 '13 at 11:42
  • @BillKarwin Figured I would update this post with the final result. Showing me that PHP had array_intersect_key() brought me to array_diff_key(). In the end the code stayed the same, just nicer to look at. Which ends up building a array of keys and values outside headers & func. IE a custom array_keys() and array_values(). – Esoterica Nov 22 '13 at 03:13