2

Let's say that I have a php file. In it, I have some variables and some functions. For example:

$results = getResults($analytics, $profile);
printResults($results);

function getResults($analytics, $profileId) {
  return $analytics->data_ga->get(
       'ga:' . $profileId,
       '3daysAgo',
       '2daysAgo',
       'ga:sessions');
}

and

function printResults($results) {
  if (count($results->getRows()) > 0) {

    $profileName = $result->getProfileInfo()->getProfileName();

    $rows = $results->getRows();
    $sessions = $rows[0][0];

    // Print the results.
    print $sessions;
  } else {
    print "No results found.\n";
  }
}

Now, how could I loop through these, changing both variable and function names dynamically?

So (pseudocode, hope it is clear what it is trying to do)

for($i=2; $i<30; $i++){
    $results = $results$i = getResults$i($analytics, $profile);
    printResults$i($results$i);

    function getResults$i($analytics, $profileId){
    return $analytics->data_ga->get(
    'ga:' . $profileId,
    $i'daysAgo',
    ($i+1)'daysAgo,
    'ga:sessions');
    }
}

meaning that $results becomes $results2, $results3, etc and getResults() becomes getResults2(), getResults3(), etc and '3daysAgo' becomes 4daysAgo, 5daysAgo, etc.

Can it be done?

moi2877
  • 55
  • 10
  • 4
    Do you want to create dynamic functions? May I know the reason because I am not getting why you want dynamic multiple methods although one method can work perfectly. – dipmala Mar 21 '18 at 13:41
  • Why do you want to create variables that you cannot access the usual way? – Nico Haase Mar 21 '18 at 13:51

3 Answers3

0

I'd use an array to store the results:

$results_array = array();

then we can use an array of functions params to pass (just as an example - not your script):

$data = array(
    'one' => array('param1' => $some_data, 'param2' => $some_other_data),
    'two' => array('param1' => $more_data, 'param2' => $some_more_data),
);

foreach ($data as $k => $v)
{
     $results_array[] = my_fnc_name($v['param1'], $v['param2']);
}

this will add in (via a numeric index) the function return value into $results_array

treyBake
  • 6,440
  • 6
  • 26
  • 57
0

you can change variable name using this function:

   function get_var_name($var) {
        foreach($GLOBALS as $var_name => $value) {
            if ($value === $var) {
                return $var_name;
            }
        }

        return false;
    }

and if you want to change variable name $request with $request1 you can do:

${get_var_name($request).'1'} = $request;

and if you want to create function dynamically:

  $function = create_function("/* comma separated arguments*/", "/*code as string*/);

and call it:

$function();
godot
  • 3,422
  • 6
  • 25
  • 42
0

#Using lambda function

$printResults$i= function($analytics, $profile) {
    //perform your logic
};

call_user_func_array($printResults$i, array($analytics, $profile));

Ref :: call_user_func_array

Dimag Kharab
  • 4,439
  • 1
  • 24
  • 45