Never been to DZone Snippets before?

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

About this user

Alex Williams http://www.alexwilliams.ca

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

PHP: Dynamically call a function from list of allowed actions

This function simply calls another function based on the argument, assuming that argument ($action) is in the "allowed" list of 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
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS