views:

40

answers:

2

I'm making a simple tool to cache function results

It look like:

global $function_results;
$function_results = array();

function getMembers($conditions, $default = array('order' => 'name', array('abc', 'def'))){

    //****Help need from here******
    //make unique id from parameters value and function name
    //ex: $uid;
    //****to here******

    global $function_results;
    if(isset($function_results[$uid])) return $function_results[$uid];
    else{
        //do function logic...
    }
}

(the function and its parameters are just an example)

Any suggestions?

+2  A: 

I suppose $conditions is an array of some values and you want to create a unique identifier for each variant of that array? There is several ways to do that, eg.:

$uid = md5(serialize($conditions));
Crozin
Yes, that what I need. I think I will use func_get_args to return all parameters of functions. Should I use serialize or json_encode?
StoneHeart
`serialize()` but in fact in that case both would do the job.
Crozin
+2  A: 

Here is your missing code:

$numargs = func_num_args();
$arg_list = func_get_args();
$md5this = '';
for ($i = 0; $i < $numargs; $i++) {
    $md5this .= $i.':'.serialize($arg_list[$i]).';';
}
$uid = md5($md5this);

For your reference: http://php.net/manual/en/function.func-get-args.php

Alec Smart
$arg_list[$i] will return "Array" string if it is array.
StoneHeart
@StoneHeart, added serialize. Its complete now.
Alec Smart
Why we don't use$arg_list = func_get_args();$uid = serialize($arg_list);
StoneHeart
@StoneHeart you could do that too. But suppose two parameters have '' as output then it will equal to another output which has '' as single parameter.
Alec Smart