problem with foreach loop..
Pok4 opened this issue · 5 comments
Pok4 commented
hi guys,
i have a problem about events in foreach loop...
I have this:
$template_events = [
'core_event_head_append'=>'template_event_head_append',
'core_event_after_header'=>'template_event_after_header',
'core_event_before_header'=>'template_event_before_header',
'core_event_after_slider'=>'template_event_after_slider',
'core_event_after_chat'=>'template_event_after_chat',
'core_event_before_left_menu'=>'template_event_before_left_menu',
'core_event_after_left_menu'=>'template_event_after_left_menu',
'core_event_before_right_menu'=>'template_event_before_right_menu',
'core_event_after_right_menu'=>'template_event_after_right_menu',
'core_event_after_news'=>'template_event_after_news',
'core_event_after_comments'=>'template_event_after_comments',
'core_event_before_footer'=>'template_event_before_footer',
'core_event_after_footer'=>'template_event_after_footer',
'core_event_inside_multipurpose_menu'=>'template_event_inside_multipurpose_menu',
'core_event_before_dropbox'=>'template_event_before_dropbox',
'core_event_before_files'=>'template_event_before_files',
'core_event_before_upload_videos'=>'template_event_before_upload_videos',
'core_event_after_contact_description'=>'template_event_after_contact_description',
'core_event_inside_custom_menu'=>'template_event_inside_custom_menu',
'core_event_inside_custom_w_menu'=>'template_event_inside_custom_w_menu',
'core_event_inside_head_ready_front'=>'template_event_inside_head_ready_front',
//admin events
'core_admin_event_ext_pages'=>'template_admin_event_ext_pages',
'core_admin_event_custom_pages_ext'=>'template_admin_event_custom_pages_ext',
'core_admin_event_after_home'=>'template_admin_event_after_home',
'core_admin_event_before_home'=>'template_admin_event_before_home',
'core_admin_event_inside_script_tag'=>'template_admin_event_inside_script_tag',
'core_admin_event_after_jquery'=>'template_admin_event_after_jquery',
'core_admin_event_head_append'=>'template_admin_event_head_append',
];
and try to loop thru all with this:
foreach($template_events as $core_event => $template_event) {
$dispatcher->on("$core_event", function($data) {
global $event_dispatcher, $template_event;
var_dump($template_event);
if(isset($event_dispatcher["$template_event"])) {
$event_dispatcher["$template_event"] .= $data;
} else {
$event_dispatcher["$template_event"] = $data;
}
});
}
but i can't... $template_event is not recognizable by on event... it return last value of array..
How to do it properly ?
Pok4 commented
i fixed it :
foreach($template_events as $core_event => $template_event) {
$dispatcher->on($core_event, function($data) use ($template_event) {
global $event_dispatcher;
if(isset($event_dispatcher[$template_event])) {
$event_dispatcher[$template_event] .= $data;
} else {
$event_dispatcher[$template_event] = $data;
}
});
}
just add a use ($template_event)
Pok4 commented
I'm wondering about performance... Is this are bad ?
WyriHaximus commented
That really depends on how often you add and remove those events.
Pok4 commented
Ok. This is all of the events. (28)
Site loading is good on litespeed web server and php 8.1.
WyriHaximus commented
Keep an eye on it when traffic increases, that's when you learn this was a good idea or not :).