PHP: Dynamically call a function from list of allowed actions
settings-config.php
1 2 $settings['actions'] = "list,view,delete,update";
Catalog-class.php
1 2 public function process_action($action) { 3 global $settings; 4 5 $allowed_actions = explode(",",$settings['actions']); 6 if (is_numeric(array_search($action, $allowed_actions))) { 7 $f_name = "product_" . $action; 8 $this->$f_name(); 9 } else { 10 $this->error_msg = 'ACTION_NOT_ALLOWED'; 11 } 12 } 13 private function product_list() { 14 } 15 private function product_view() { 16 } 17 private function product_delete() { 18 } 19 private function product_update() { 20 } 21 private function product_create() { 22 }
Even if product_create() exists, if you do process_action('create'), it will not work... it's basically like a function wrapper.. or something like that...
UPDATE: Yes I know I could have created an array like
1 $settions['actions'] = array('list','view','delete','update');but I've modified this code a bit for snippet/display purposes... anyways this is just a reminder for myself, not of actual use or interest for others... comments are welcome though :D