MyBB Community Forums

Full Version: Closure support for plugins
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Closures are objects and as such you do not need to require the plugin file to run the hook. if you look at the pull request, you will see that hooks using closures are far more simple to execute.
(2013-12-07, 04:50 PM)Euan T Wrote: [ -> ]Closures are objects and as such you do not need to require the plugin file to run the hook.
But the plugin file is included anyway to add the hooks? Or am I missing something?
Just had another look at the class. add_hook accepts an optional $file argument - it is this file that is included if it is not empty.

if($hook['file'])
{
    require_once $hook['file'];
}

$func = $hook['function'];
  
$returnargs = $func($arguments);
  
if($returnargs)
{
    $arguments = $returnargs;
}

I somehow managed to confuse myself slightly on that as that fourth parameter is a little obscure.

Also note that we should also probably expand the class slightly further to allow the use of class methods instead of just functions. As is, I don't believe this works. This would be achieved by passing an array($object, 'functionName') to add_hook's function parameter.
There are probably not many plugins using that parameter. Wink

I agree about class methods. Maybe it should support create_function(), too.
I'm working on a new commit for class methods now, will have to look into how create_function works though as I've honestly never used it.

EDIT: This should work for class/object methods passed via array to add_hook: https://github.com/MyBBStuff/mybb/commit...a775ab17ce
Pages: 1 2