简体   繁体   中英

php - create function name with variable

I have some code like this

$form_names = GFFormsModel::get_forms();
foreach ($form_names as $form) {
  add_action("gform_pre_submission_".$form->id, "format_ecp_event_meta_from_gravity");

  function format_ecp_event_meta_from_gravity(){

  }
}

Since i'm using foreach loop functions get duplicated . So is there a way to make the function unique using $form->id ?

I mean i want the function names like this

function format_ecp_event_meta_from_gravity_{$form->id}(){

}

Can someone help me? Thanks

Use closures:

$form_names = GFFormsModel::get_forms();
foreach ($form_names as $form) {
    $func = function() {
        ...
    };
    add_action("gform_pre_submission_".$form->id, $func);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM