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

  • 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['actions'] = "list,view,delete,update";

public function process_action($action) {
	global $settings;
	$allowed_actions = explode(",",$settings['actions']);
	if (is_numeric(array_search($action, $allowed_actions))) {
		$f_name = "product_" . $action;
	} 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