MyBB Community Forums

Full Version: class_plugins improvements
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi i want to suggest new improvements to the pluginSystem class.

1. have the ability to call a class method
$plugins->add_hook("global_start", array(a, "a"));

You'll have only to edit a bit the caching of the $hooks array as add_hook expects that $function is a string, but what about if it is an array? Anyway it should be very easy to implement.
		$this->hooks[$hook][$priority][$function] = array(
			"function" => $function,
			"file" => $file
		);
As you can see the key of the array has the value of $function, but what about if it is an array?

2. second is to check if the function exists
	function add_hook($hook, $function, $priority=10, $file="")
	{
		// Check to see if we already have this hook running at this priority
		if(is_array($this->hooks[$hook][$priority][$function]))
		{
			return true;
		}

		// Add the hook
		if(function_exists($function))
		{
			$this->hooks[$hook][$priority][$function] = array(
				"function" => $function,
				"file" => $file
			);
			return true;
		}
	}

Easy to do but useful IMO. Wink
You can simply use a function wrapper to call your class. 99.999% of the time this would be unnecessary right now.
Yep, i could use a function wrapper, this is what i do actually for a plugin asked by one, but i think that the plugin system should allow us to call a class method without making workaround (see the function wrapper).

Regards
One thing that would be interesting though, is if each plugin is a class that implements a plugin interface...like...

interface IPlugin
{
    public static function _info();
    public static function _activate();
    public static function _deactivate();
}

Then each plugin would be like...

<?php
//myplugintest.php
$plugins->add_hook("global_start", "myplugintest", "run_global_start");
class plugin_myplugintest implements IPlugin
{
    public static function _info()
    {
        return array (...);
    }
    public static function _activate()
    {
        global $db;
        // whatever
    }
    public static function _deactivate()
    {
        global $db;
        // whatever
    }
    public static function run_global_start()
    {
        //whatever
    }
}

Then there wouldn't be any problems with function naming conflicts.
^ There'd still be naming conflicts if:
1) The plugin author defines functions outside of the plugin
2) Two authors decide to use the same name for a plugin

Basically, if authors prefix their function names, there shouldn't really be any issues. Classes may seem a little nicer (don't have to use prefixes), but that's about as far as they go. There's also nothing stopping a plugin developer from using classes either >_>



@flash.tato: I somewhat like the first suggestion, though I think it's more difficult that you probably imagined - you can't really serialize the array, so you're a little stuck on that one... (the function name is used as a key to help prevent duplicates - if we remove that, we can use arrays, but then, we're removing a feature...)
For the second, I personally wouldn't - as a plugin developer, I'd prefer to see a big warning if I mistyped a function name, rather than flailing around trying to track it down.
So you could use as key:
classname_method

$function = get_class($obj) . "_" . "foo";
flash.tato Wrote:So you could use as key:
classname_method

$function = get_class($obj) . "_" . "foo";
That doesn't work because you need the instance of the object to call, not the classname.
Yes, you can store an object reference in the array, however, that also means that you cannot use multiple instances of a class...
That is true, didn't thought about this.