MyBB Ideas

The MyBB Ideas site allows users to suggest new features and changes in MyBB and the MyBB Group to easily review them to decide what direction MyBB should head in. Search existing ideas.

Plugin System Redesign

45 votes

Submitted by Aries-Belgium, 8th June 2007, 2:22:14 PM

Now that OOP is also finding its way to the PHP world, I would like to suggest another approach of the plugin system of MyBB, namely a class-based pluginsystem. By using classes you eliminate function-name conflicts and instead of hooking certain functions to certain predefined hook, you just give the function the same name as the hook.
--- CODE ------
class ExamplePlugin extends MyBBPlugin {
function info()
{
return array(
'name' => "ExamplePlugin",
'description' => "An example plugin"
);
}

function activate()
{
// things to do when you activate the plugin
}

function deactivate()
{
// things to do when you deactivate
}

function postbit($post)
{
$post['message'] = "Hello World";
}
}
---------------

Getting the hooks can then be done automatically:
--- CODE -------
$plugin_hooks = array()
foreach(get_declared_classes() as $classname)
{
// is_active() = MyBB function
if(is_active($classname) && is_subclass_of(new $classname(), "MyBBPlugin"))
{
foreach(get_class_methods($classname) as $method)
{
// $method is hookname
$plugin_hooks[$method][] = $classname;
}
}
}
---------------

When MyBB needs to run a hook, all it needs to be done is loop through the $plugin_hooks array for that hook:
--- CODE --------
foreach($plugin_hooks['postbit'] as $classname)
{
$obj = new $classname();
$obj->postbit(&$post);
}
------------------

7 Comments

  1. I like the idea, but I have a question (i'm not very used to work with objects in php): Is that code as fast/optimum (measuring execution time and memory consumption) as current implementation of the plugins? Is it as powerful as current implementation? (i mean if you can do the same things (as easyly) as before)

    Choli, 8th June 2007, 11:09:25 PM
  2. It's even more easily, because you don't need to hook the functions to their hooks. My implementation should do that automatically. And in terms of speed: a great advantage is that you can store objects in an array and that you can serialize that array and store it in a session, database, file, ... for caching purposes.

    Aries-Belgium, 9th June 2007, 8:34:31 AM
  3. Very nice idea. Actually, I'll definitely look into it for a later version.

    Ryan Gordon, 10th June 2007, 3:41:57 AM
  4. Yeah it looks like a good idea.

    DennisTT, 11th June 2007, 6:49:45 AM
  5. Yes but how will this effect the many existing plugins already? I would certainly be rather upset if I had to rewrite large portions of code to make it compatible with a new system. Your implementation might be great but one has to consider other aspects too.

    labrocca, 25th June 2007, 6:00:00 AM
  6. We could of course make them both be valid formats. :)

    CraKteR, 2nd July 2007, 9:20:29 PM
  7. I like the idea of going object-oriented, but you still have to register which hooks a particular plugin is interested in.

    laie_techie, 23rd September 2007, 9:29:33 PM

Post a Comment

Before you can post a comment you must be a registered member of the MyBB Community Forums.

If you already have an account, log-in to it to post a comment or if you don't register a new account.