Skip to content

Instantly share code, notes, and snippets.

@geilt
Created April 24, 2013 23:12
Show Gist options
  • Save geilt/5456354 to your computer and use it in GitHub Desktop.
Save geilt/5456354 to your computer and use it in GitHub Desktop.
Add Action and Do Action for any system using Globals. Inspired by Wordpress but custom Coded.
<?php
// Queue: Queues a function or object method to be run during do_action
function add_action($action, $function, $priority = 10, $args = array()){
$GLOBALS['actions'][$action][$priority][] = array('function' => $function, 'args' => $args);
}
// Hook: Runs a function or a object method based on add_action
function do_action($action){
$done = '';
if(!empty($GLOBALS['actions'][$action])):
foreach($GLOBALS['actions'][$action] as $priorities):
foreach($priorities as $do):
if(is_array($do['function'])):
$object = $do['function'][0];
$method = $do['function'][1];
if(method_exists($object, $method)):
if(!empty($do['args'])):
$done .= call_user_func_array( array($object, $method), $do['args'] );
else:
$done .= $object->$method();
endif;
if(!empty($done)) $done .= PHP_EOL;
endif;
else:
$function = $do['function'];
if(function_exists($function)):
if(!empty($do['args'])):
$done .= call_user_func_array( $function , $do['args'] );
else:
$done .= $function();
endif;
if(!empty($done)) $done .= PHP_EOL;
endif;
endif;
endforeach;
endforeach;
endif;
if(!empty($done)) echo $done;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment