3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Hook class file * * @category Include * @package Slimium\Module\Core * @author Jakub Ďuraš <jkblmr@gmail.com> */ /** * Hook class- class for working with hooks * * -100 - lowest priority <br> * 100+ - highest priority <br> * 50 - default priority <br> * if the priority is already taken, the nearest free lower one is taken */ class hook { /** * List of hooks * @var array */ private static $hooks = []; /** * Call hooks and don't pass any returned value * @param string $hook_enviroment can be module name or other enviroment in which hook is located * @param string $hook_name name * @param mixed $argument argument to pass to hook * @return boolean TRUE if there are any hooks assigned and they were executed, FALSE in there are no hooks assigned */ public static function call($hook_enviroment, $hook_name, $argument = NULL) { //If there is any hooked function if (isset(self::$hooks[$hook_enviroment][$hook_name])) { //Sort array krsort(self::$hooks[$hook_enviroment][$hook_name]); //Loop thorugh array foreach (self::$hooks[$hook_enviroment][$hook_name] as $hook_function) { $hook_function($argument); } return true; }else{ return false; } } /** * Call hooks, let argument go through filtering and return filtered argument * @param string $hook_enviroment can be module name or other enviroment in which hook is located * @param string $hook_name name * @param mixed $argument argument to pass to hook * @return mixed filtered argument */ public static function filter($hook_enviroment, $hook_name, $argument = NULL) { //If there is any hooked function if (isset(self::$hooks[$hook_enviroment][$hook_name])) { //Sort array krsort(self::$hooks[$hook_enviroment][$hook_name]); $filtered = $argument; $first = true; //Loop thorugh array foreach (self::$hooks[$hook_enviroment][$hook_name] as $hook_function) { if($first){ $filtered = $hook_function($argument); $first = false; }else{ $filtered = $hook_function($argument, $filtered); } } return $filtered; } } /** * Add hook * @param string $hook_enviroment can be module name or other enviroment in which hook is located * @param string $hook_name name * @param callable $function name of the function of anonymous function to call * @param callable $priority priority of the hook, can be anything from 1(executed as last) to 100+(executed as first) if priority is already taken, the nearest free lower one is taken * @return integer priority at which hook was added */ public static function add($hook_enviroment, $hook_name, $function, $priority = 50) { //If we do not need to loop through prirorities if(!isset(self::$hooks[$hook_enviroment][$hook_name][$priority])){ self::$hooks[$hook_enviroment][$hook_name][$priority] = $function; return $priority; } //Loop through hooked functions to find nearest free priority for ($priority; $priority > -100; $priority--) { if(!isset(self::$hooks[$hook_enviroment][$hook_name][$priority])){ self::$hooks[$hook_enviroment][$hook_name][$priority] = $function; break; } } return $priority; } /** * Remove hook at certain priority * @param string $hook_enviroment can be module name or other enviroment in which hook is located * @param string $hook_name name * @param callable $priority priority of the hook to delete * @return boolean TRUE */ public static function remove($hook_enviroment, $hook_name, $priority) { unset(self::$hooks[$hook_enviroment][$hook_name][$priority]); return true; } /** * Remove hook all hooks of some name and in som enviroment * @param string $hook_enviroment can be module name or other enviroment in which hook is located * @param string $hook_name name * @return boolean TRUE */ public static function remove_all($hook_enviroment, $hook_name) { self::$hooks[$hook_enviroment][$hook_name] = []; return true; } /** * Is anything hooked at certain priority * @param string $hook_enviroment can be module name or other enviroment in which hook is located * @param string $hook_name name * @param callable $priority priority of the hook * @return boolean TRUE or FALSE */ public static function is_hooked($hook_enviroment, $hook_name, $priority) { if(isset(self::$hooks[$hook_enviroment][$hook_name][$priority])){ return true; }else{ return false; } } } hook::add('main', 'init', function(){$var = 'foo';}); hook::add('main', 'init', function(){$var = 'foo';}); hook::add('main', 'init', function(){$var = 'foo';}); hook::add('main', 'init', function(){$var = 'foo';}); hook::add('main', 'init', function(){$var = 'foo';}); hook::add('main', 'init', function(){$var = 'foo';}); hook::add('main', 'init', function(){$var = 'foo';}); hook::add('main', 'init', function(){$var = 'foo';}); hook::add('main', 'init', function(){$var = 'foo';}); hook::add('main', 'init', function(){$var = 'foo';}); hook::call('main', 'init');
Output for git.master, git.master_jit, rfc.property-hooks

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
45.74 ms | 401 KiB | 8 Q