PHP: Dynamically Call A Function From List Of Allowed Actions
Join the DZone community and get the full member experience.
Join For FreeThis function simply calls another function based on the argument, assuming that argument ($action) is in the "allowed" list of actions.
settings-config.php
$settings['actions'] = "list,view,delete,update";
Catalog-class.php
public function process_action($action) {
global $settings;
$allowed_actions = explode(",",$settings['actions']);
if (is_numeric(array_search($action, $allowed_actions))) {
$f_name = "product_" . $action;
$this->$f_name();
} else {
$this->error_msg = 'ACTION_NOT_ALLOWED';
}
}
private function product_list() {
}
private function product_view() {
}
private function product_delete() {
}
private function product_update() {
}
private function product_create() {
}
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 $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
PHP
Opinions expressed by DZone contributors are their own.
Comments