MyBB Community Forums

Full Version: Allow anonymous function to pluginSystem::add_hook
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I tried to use

$plugins->add_hook('pre_output_page', function($page){
        return 'This is a test';
});

then I tried
$plugins->add_hook('pre_output_page', 'MyClass::pre_output');
But it throws error.
I like to use because I'm looking for clean code and OOP.

I tried to "fix" it, I've changed http://pastebin.com/eJ50iKN0 to http://pastebin.com/JcPC6Duj
And it works fine to me but it have trouble when you use a function passed by reference

I've suggested that on ideas.mybb.com but no one replied Confused

Edit: You can add that to the mybb 1.8 (because 2.0 will be re-writed)
And what about the people that don't have PHP 5.3? Anonymous functions are only 5.3+.
Exactly. Hosts have not upgraded yet. Too many people are too lazy to upgrade WordPress and other scripts that hosts can't move forward because of the outcry.
(2012-04-08, 05:38 PM)MattRogowski Wrote: [ -> ]And what about the people that don't have PHP 5.3? Anonymous functions are only 5.3+.
But MyBB can check if the function It's anonymous or not using is_string then you can put an anonymous function or the name of one

Edit: using is_callable and the code I've passed on pastebin you can use name of functions too
Bump, bump everywhere
and what is the purpose of doing this? how do you expect to utilize the functionality?
For example, I like to use classes to keep the mybb variable for example as self::$mybb->input... and for doing something like
$plugins->add_hook('pre_output_page', function($page) use(&$mybb, &$templates){
// Stuff
});
I don't know if "use" it's more faster than "global" (I believe that it is)
how can use() be faster than a global variable when the user() function itself has to make use of the already existing global?

is the purpose of use() not to be able to reassign one object into/as another object, but your example is not working that way.
I still saying, I think it's more clean using anonymous function and using classes