DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

PHP: Dynamically Call A Function From List Of Allowed Actions

01.03.2007
| 8282 views |
  • submit to reddit
        This 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