MyBB Community Forums

Full Version: Objects in plugins - MyBB 1.6.5
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello,
I wrote some plugins, usually I use objects or static methods in my plugins.
There is a big problem with MyBB 1.6.5, of course I read this:
http://community.mybb.com/thread-106729.html

but it isn't a problem for me.
Bad thing is this in inc/class_plugins.php:

$returnargs = $func($arguments);

How can I use object when I add my functions to hook?
Until today I've made that:

Inside plugin object:
$plugins->hooks["hook_name"][10]["my_func_name"] = array("function" => array($this, "my_func_name"));

example from view unread posts:
$plugins->hooks["search_start"][10]["up_doSearch"] = array("function" => array($this, "doSearch"));

Or static method:
$plugins->add_hook('hook_name','class_name::method_name');

example from fast quote:
$plugins->add_hook('postbit','fastQuote::addButton');

Now, both methods are invalid and display errors - it's normal, because my code is good for call_user_func_array, not for currently solution.

So, how can I use objects in my plugins to MyBB?
It must be possible, otherwise, I can throw into the trash a few months to correct and rewrite plugins Sad
You could call a normal function which calls the object function.

$plugins->add_hook('postbit','fqab'); 

function fqab()
{
return fastQuote::addButton;
}

Sorry, but it's stupid.

My mod Notify plugin constructor:

    /**
     * Constructor - add plugin hooks
     */
    public function __construct()
    {
        global $plugins;

        // Add all hooks
        $plugins->hooks["moderation_start"][10]["mn_init"] = array("function" => array($this, "init"));
        $plugins->hooks["class_moderation_close_threads"][10]["mn_closeThreads"] = array("function" => array($this, "closeThreads"));
        $plugins->hooks["class_moderation_open_threads"][10]["mn_openThreads"] = array("function" => array($this, "openThreads"));
        $plugins->hooks["class_moderation_delete_thread"][10]["mn_deleteThread"] = array("function" => array($this, "deleteThread"));
        $plugins->hooks["class_moderation_stick_threads"][10]["mn_stickyThreads"] = array("function" => array($this, "stickyThreads"));
        $plugins->hooks["class_moderation_unstick_threads"][10]["mn_unstickyThreads"] = array("function" => array($this, "unstickyThreads"));
        $plugins->hooks["class_moderation_approve_threads"][10]["mn_approveThreads"] = array("function" => array($this, "approveThreads"));
        $plugins->hooks["class_moderation_unapprove_threads"][10]["mn_unapproveThreads"] = array("function" => array($this, "unapproveThreads"));
        $plugins->hooks["class_moderation_change_thread_subject"][10]["mn_threadSubject"] = array("function" => array($this, "threadSubject"));
        $plugins->hooks["class_moderation_delete_poll"][10]["mn_deletePoll"] = array("function" => array($this, "deletePoll"));
        $plugins->hooks["class_moderation_delete_post_start"][10]["mn_deletePost"] = array("function" => array($this, "deletePost"));
        $plugins->hooks["class_moderation_copy_thread"][10]["mn_copyThread"] = array("function" => array($this, "copyThread"));
        $plugins->hooks["class_moderation_move_simple"][10]["mn_moveRedirect"] = array("function" => array($this, "moveRedirect"));
        $plugins->hooks["class_moderation_move_thread_redirect"][10]["mn_moveRedirect"] = array("function" => array($this, "moveRedirect"));
        $plugins->hooks["class_moderation_move_threads"][10]["mn_moveThreads"] = array("function" => array($this, "moveThreads"));
        $plugins->hooks["class_moderation_merge_threads"][10]["mn_mergeThreads"] = array("function" => array($this, "mergeThreads"));
        $plugins->hooks["class_moderation_split_posts"][10]["mn_splitPosts"] = array("function" => array($this, "splitPosts"));
        $plugins->hooks["class_modnotify_moderation_approve_posts"][10]["mn_approvePosts"] = array("function" => array($this, "approvePosts"));
        $plugins->hooks["class_modnotify_moderation_unapprove_posts"][10]["mn_unapprovePosts"] = array("function" => array($this, "unapprovePosts"));
    }

Can you imagine the creation function for each hook?
I think you should add new argument to add hook method, which will determine how it should be supported - by default, a new method, but with the possibility of using the previous solution.
In reality you are using something that was never supported. Yes the code allowed it, but if you read through the official plugin documentation you will find nothing even remotely suggesting you should do things this way. At least not that I'm aware of, and I have most of the plugin pages on the wiki bookmarked for quick references.
I do not use it because OOP is nice - I use it, because it simplifies code, because I can easily adjust the code to a few situations.
In the last six months I rewrote all plug-ins for OOP: they operate efficiently, are more reliable. Now I can put all this time in the trash... so I will throw to the trash MyBB too...

MyBB is unpredictable - you should not make such changes within a single branch.
Instead of having a childish fit about it and getting angry maybe you could discuss it rationally with us? One reason we changed this was because call_user_func_array() uses a lot more overhead. Is that really offset by OOP plugins? In the long run I really don't know. But you still cannot be upset for us removing something that was never intended functionality in the first place. Or rather you can but no one will be phased by it.
Oh, and also how many weeks have you had to test this with the changes illustrated in the thread about the plugin changes? Next time please prepare ahead of time.
(2011-11-25, 06:26 PM)lukasamd Wrote: [ -> ]Can you imagine the creation function for each hook?

Maybe like this?

public function __construct()
{
    global $plugins;

    $plugins->add_hook("some_hook", create_function('&$arg','global $YourObject; $YourObject->foobar($arg);'));
}

It requires the object to be available through the global scope though. Which is not an unsolvable problem but not particularly nice in any case.
@frostschutz:
Thanks for code, I tried, but it doesn't work - I think because my plugin object isn't global in pluginSystem method, but I'm not sure.

@Dylan M.:
0 week. I didn't do that, beacuse I shouldn't do that - it's the same branch. I will understand if it was a completely new version, new branch (for example. MyBB 1.8). But it's the same, and you are introducing so big changes... Why phpBB developers can extend their script, but don't impede modders life? Why mods for phpBB 3.0.0 works fine with 3.0.9?
If you decide to make such changes, you should ensure backward compatibility (additional argument when we run hooks, for example of course).

I know, that according to the wiki I should use standard functions, but... See some of my plugins - you'll see why the objects are better. The best example is View Unread Posts.

BTW. Changes in call by ref / non-ref aren't problems, I can fix it quick. The problem is that you block modders to use their own objects.
(2011-11-26, 10:11 AM)lukasamd Wrote: [ -> ]@frostschutz:
Thanks for code, I tried, but it doesn't work - I think because my plugin object isn't global in pluginSystem method, but I'm not sure.

Yeah sure, but it's easy to put objects in global...

(2011-11-26, 10:11 AM)lukasamd Wrote: [ -> ]I know, that according to the wiki I should use standard functions, but... See some of my plugins - you'll see why the objects are better. The best example is View Unread Posts.

I've looked at your best example plugin but I see nothing in there that warrants the use of classes/objects for your plugin. Maybe you want to elaborate? I agree in general that objects would be better, but that's just not how MyBB works at the moment. Although MyBB does make use of classes here and there, mostly it's just flat functions with tons of stuff in the global scope. That's just how it is.
Pages: 1 2